home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / aspisrc.zip / REGEX.C < prev    next >
C/C++ Source or Header  |  1992-01-26  |  87KB  |  2,784 lines

  1. /* Extended regular expression matching and search library.
  2.    Copyright (C) 1985, 1989-90 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18.  
  19. /* To test, compile with -Dtest.  This Dtestable feature turns this into
  20.    a self-contained program which reads a pattern, describes how it
  21.    compiles, then reads a string and searches for it.
  22.    
  23.    On the other hand, if you compile with both -Dtest and -Dcanned you
  24.    can run some tests we've already thought of.  */
  25.  
  26.  
  27. #ifdef emacs
  28.  
  29. /* The `emacs' switch turns on certain special matching commands
  30.   that make sense only in emacs. */
  31.  
  32. #include "config.h"
  33. #include "lisp.h"
  34. #include "buffer.h"
  35. #include "syntax.h"
  36.  
  37. #else  /* not emacs */
  38.  
  39. #if defined (USG) || defined (STDC_HEADERS)
  40. #ifndef BSTRING
  41. #include <string.h>
  42. #define bcopy(s,d,n)    memcpy((d),(s),(n))
  43. #define bcmp(s1,s2,n)    memcmp((s1),(s2),(n))
  44. #define bzero(s,n)    memset((s),0,(n))
  45. #endif
  46. #endif
  47.  
  48. #ifdef STDC_HEADERS
  49. #include <stdlib.h>
  50. #else
  51. char *malloc ();
  52. char *realloc ();
  53. #endif
  54.  
  55. /* Make alloca work the best possible way.  */
  56. #ifdef __GNUC__
  57. #define alloca __builtin_alloca
  58. #else
  59. #ifdef sparc
  60. #include <alloca.h>
  61. #else
  62. #ifdef _AIX
  63. #pragma alloca
  64. #else
  65. char *alloca ();
  66. #endif
  67. #endif
  68. #endif
  69.  
  70.  
  71. /* Define the syntax stuff, so we can do the \<, \>, etc.  */
  72.  
  73. /* This must be nonzero for the wordchar and notwordchar pattern
  74.    commands in re_match_2.  */
  75. #ifndef Sword 
  76. #define Sword 1
  77. #endif
  78.  
  79. #define SYNTAX(c) re_syntax_table[c]
  80.  
  81.  
  82. #ifdef SYNTAX_TABLE
  83.  
  84. char *re_syntax_table;
  85.  
  86. #else /* not SYNTAX_TABLE */
  87.  
  88. static char re_syntax_table[256];
  89.  
  90.  
  91. static void
  92. init_syntax_once ()
  93. {
  94.    register int c;
  95.    static int done = 0;
  96.  
  97.    if (done)
  98.      return;
  99.  
  100.    bzero (re_syntax_table, sizeof re_syntax_table);
  101.  
  102.    for (c = 'a'; c <= 'z'; c++)
  103.      re_syntax_table[c] = Sword;
  104.  
  105.    for (c = 'A'; c <= 'Z'; c++)
  106.      re_syntax_table[c] = Sword;
  107.  
  108.    for (c = '0'; c <= '9'; c++)
  109.      re_syntax_table[c] = Sword;
  110.  
  111.    done = 1;
  112. }
  113.  
  114. #endif /* SYNTAX_TABLE */
  115. #endif /* emacs */
  116.  
  117. /* We write fatal error messages on standard error.  */
  118. #include <stdio.h>
  119.  
  120. /* isalpha(3) etc. are used for the character classes.  */
  121. #include <ctype.h>
  122. /* Sequents are missing isgraph.  */
  123. #ifndef isgraph
  124. #define isgraph(c) (isprint((c)) && !isspace((c)))
  125. #endif
  126.  
  127. /* Get the interface, including the syntax bits.  */
  128. #include "regex.h"
  129.  
  130.  
  131. /* These are the command codes that appear in compiled regular
  132.    expressions, one per byte.  Some command codes are followed by
  133.    argument bytes.  A command code can specify any interpretation
  134.    whatsoever for its arguments.  Zero-bytes may appear in the compiled
  135.    regular expression.
  136.    
  137.    The value of `exactn' is needed in search.c (search_buffer) in emacs.
  138.    So regex.h defines a symbol `RE_EXACTN_VALUE' to be 1; the value of
  139.    `exactn' we use here must also be 1.  */
  140.  
  141. enum regexpcode
  142.   {
  143.     unused=0,
  144.     exactn=1, /* Followed by one byte giving n, then by n literal bytes.  */
  145.     begline,  /* Fail unless at beginning of line.  */
  146.     endline,  /* Fail unless at end of line.  */
  147.     jump,     /* Followed by two bytes giving relative address to jump to.  */
  148.     on_failure_jump,     /* Followed by two bytes giving relative address of 
  149.                 place to resume at in case of failure.  */
  150.     finalize_jump,     /* Throw away latest failure point and then jump to 
  151.                 address.  */
  152.     maybe_finalize_jump, /* Like jump but finalize if safe to do so.
  153.                 This is used to jump back to the beginning
  154.                 of a repeat.  If the command that follows
  155.                 this jump is clearly incompatible with the
  156.                 one at the beginning of the repeat, such that
  157.                 we can be sure that there is no use backtracking
  158.                 out of repetitions already completed,
  159.                 then we finalize.  */
  160.     dummy_failure_jump,  /* Jump, and push a dummy failure point. This 
  161.                 failure point will be thrown away if an attempt 
  162.                             is made to use it for a failure. A + construct 
  163.                             makes this before the first repeat.  Also
  164.                             use it as an intermediary kind of jump when
  165.                             compiling an or construct.  */
  166.     succeed_n,     /* Used like on_failure_jump except has to succeed n times;
  167.             then gets turned into an on_failure_jump. The relative
  168.                     address following it is useless until then.  The
  169.                     address is followed by two bytes containing n.  */
  170.     jump_n,     /* Similar to jump, but jump n times only; also the relative
  171.             address following is in turn followed by yet two more bytes
  172.                     containing n.  */
  173.     set_number_at,    /* Set the following relative location to the
  174.                subsequent number.  */
  175.     anychar,     /* Matches any (more or less) one character.  */
  176.     charset,     /* Matches any one char belonging to specified set.
  177.             First following byte is number of bitmap bytes.
  178.             Then come bytes for a bitmap saying which chars are in.
  179.             Bits in each byte are ordered low-bit-first.
  180.             A character is in the set if its bit is 1.
  181.             A character too large to have a bit in the map
  182.             is automatically not in the set.  */
  183.     charset_not, /* Same parameters as charset, but match any character
  184.                     that is not one of those specified.  */
  185.     start_memory, /* Start remembering the text that is matched, for
  186.             storing in a memory register.  Followed by one
  187.                     byte containing the register number.  Register numbers
  188.                     must be in the range 0 through RE_NREGS.  */
  189.     stop_memory, /* Stop remembering the text that is matched
  190.             and store it in a memory register.  Followed by
  191.                     one byte containing the register number. Register
  192.                     numbers must be in the range 0 through RE_NREGS.  */
  193.     duplicate,   /* Match a duplicate of something remembered.
  194.             Followed by one byte containing the index of the memory 
  195.                     register.  */
  196.     before_dot,     /* Succeeds if before point.  */
  197.     at_dot,     /* Succeeds if at point.  */
  198.     after_dot,     /* Succeeds if after point.  */
  199.     begbuf,      /* Succeeds if at beginning of buffer.  */
  200.     endbuf,      /* Succeeds if at end of buffer.  */
  201.     wordchar,    /* Matches any word-constituent character.  */
  202.     notwordchar, /* Matches any char that is not a word-constituent.  */
  203.     wordbeg,     /* Succeeds if at word beginning.  */
  204.     wordend,     /* Succeeds if at word end.  */
  205.     wordbound,   /* Succeeds if at a word boundary.  */
  206.     notwordbound,/* Succeeds if not at a word boundary.  */
  207.     syntaxspec,  /* Matches any character whose syntax is specified.
  208.             followed by a byte which contains a syntax code,
  209.                     e.g., Sword.  */
  210.     notsyntaxspec /* Matches any character whose syntax differs from
  211.                      that specified.  */
  212.   };
  213.  
  214.  
  215. /* Number of failure points to allocate space for initially,
  216.    when matching.  If this number is exceeded, more space is allocated,
  217.    so it is not a hard limit.  */
  218.  
  219. #ifndef NFAILURES
  220. #define NFAILURES 80
  221. #endif
  222.  
  223. #ifdef CHAR_UNSIGNED
  224. #define SIGN_EXTEND_CHAR(c) ((c)>(char)127?(c)-256:(c)) /* for IBM RT */
  225. #endif
  226. #ifndef SIGN_EXTEND_CHAR
  227. #define SIGN_EXTEND_CHAR(x) (x)
  228. #endif
  229.  
  230.  
  231. /* Store NUMBER in two contiguous bytes starting at DESTINATION.  */
  232. #define STORE_NUMBER(destination, number)                \
  233.   { (destination)[0] = (number) & 0377;                    \
  234.     (destination)[1] = (number) >> 8; }
  235.   
  236. /* Same as STORE_NUMBER, except increment the destination pointer to
  237.    the byte after where the number is stored.  Watch out that values for
  238.    DESTINATION such as p + 1 won't work, whereas p will.  */
  239. #define STORE_NUMBER_AND_INCR(destination, number)            \
  240.   { STORE_NUMBER(destination, number);                    \
  241.     (destination) += 2; }
  242.  
  243.  
  244. /* Put into DESTINATION a number stored in two contingous bytes starting
  245.    at SOURCE.  */
  246. #define EXTRACT_NUMBER(destination, source)                \
  247.   { (destination) = *(source) & 0377;                    \
  248.     (destination) += SIGN_EXTEND_CHAR (*(char *)((source) + 1)) << 8; }
  249.  
  250. /* Same as EXTRACT_NUMBER, except increment the pointer for source to
  251.    point to second byte of SOURCE.  Note that SOURCE has to be a value
  252.    such as p, not, e.g., p + 1. */
  253. #define EXTRACT_NUMBER_AND_INCR(destination, source)            \
  254.   { EXTRACT_NUMBER (destination, source);                \
  255.     (source) += 2; }
  256.  
  257.  
  258. /* Specify the precise syntax of regexps for compilation.  This provides
  259.    for compatibility for various utilities which historically have
  260.    different, incompatible syntaxes.
  261.    
  262.    The argument SYNTAX is a bit-mask comprised of the various bits
  263.    defined in regex.h.  */
  264.  
  265. int
  266. re_set_syntax (syntax)
  267.   int syntax;
  268. {
  269.   int ret;
  270.  
  271.   ret = obscure_syntax;
  272.   obscure_syntax = syntax;
  273.   return ret;
  274. }
  275.  
  276. /* Set by re_set_syntax to the current regexp syntax to recognize.  */
  277. int obscure_syntax = 0;
  278.  
  279.  
  280.  
  281. /* Macros for re_compile_pattern, which is found below these definitions.  */
  282.  
  283. #define CHAR_CLASS_MAX_LENGTH  6
  284.  
  285. /* Fetch the next character in the uncompiled pattern, translating it if
  286.    necessary.  */
  287. #define PATFETCH(c)                            \
  288.   {if (p == pend) goto end_of_pattern;                    \
  289.   c = * (unsigned char *) p++;                        \
  290.   if (translate) c = translate[c]; }
  291.  
  292. /* Fetch the next character in the uncompiled pattern, with no
  293.    translation.  */
  294. #define PATFETCH_RAW(c)                            \
  295.  {if (p == pend) goto end_of_pattern;                    \
  296.   c = * (unsigned char *) p++; }
  297.  
  298. #define PATUNFETCH p--
  299.  
  300.  
  301. /* If the buffer isn't allocated when it comes in, use this.  */
  302. #define INIT_BUF_SIZE  28
  303.  
  304. /* Make sure we have at least N more bytes of space in buffer.  */
  305. #define GET_BUFFER_SPACE(n)                        \
  306.   {                                        \
  307.     while (b - bufp->buffer + (n) >= bufp->allocated)            \
  308.       EXTEND_BUFFER;                            \
  309.   }
  310.  
  311. /* Make sure we have one more byte of buffer space and then add CH to it.  */
  312. #define BUFPUSH(ch)                            \
  313.   {                                    \
  314.     GET_BUFFER_SPACE (1);                        \
  315.     *b++ = (char) (ch);                            \
  316.   }
  317.   
  318. /* Extend the buffer by twice its current size via reallociation and
  319.    reset the pointers that pointed into the old allocation to point to
  320.    the correct places in the new allocation.  If extending the buffer
  321.    results in it being larger than 1 << 16, then flag memory exhausted.  */
  322. #define EXTEND_BUFFER                            \
  323.   { char *old_buffer = bufp->buffer;                    \
  324.     if (bufp->allocated == (1L<<16)) goto too_big;            \
  325.     bufp->allocated *= 2;                        \
  326.     if (bufp->allocated > (1L<<16)) bufp->allocated = (1L<<16);        \
  327.     bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated);    \
  328.     if (bufp->buffer == 0)                        \
  329.       goto memory_exhausted;                        \
  330.     b = (b - old_buffer) + bufp->buffer;                \
  331.     if (fixup_jump)                            \
  332.       fixup_jump = (fixup_jump - old_buffer) + bufp->buffer;        \
  333.     if (laststart)                            \
  334.       laststart = (laststart - old_buffer) + bufp->buffer;        \
  335.     begalt = (begalt - old_buffer) + bufp->buffer;            \
  336.     if (pending_exact)                            \
  337.       pending_exact = (pending_exact - old_buffer) + bufp->buffer;    \
  338.   }
  339.  
  340. /* Set the bit for character C in a character set list.  */
  341. #define SET_LIST_BIT(c)  (b[(c) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
  342.  
  343. /* Get the next unsigned number in the uncompiled pattern.  */
  344. #define GET_UNSIGNED_NUMBER(num)                     \
  345.   { if (p != pend)                             \
  346.       {                                 \
  347.         PATFETCH (c);                             \
  348.     while (isdigit (c))                         \
  349.       {                                 \
  350.         if (num < 0)                         \
  351.            num = 0;                         \
  352.             num = num * 10 + c - '0';                     \
  353.         if (p == pend)                         \
  354.            break;                             \
  355.         PATFETCH (c);                         \
  356.       }                                 \
  357.         }                                 \
  358.   }
  359.  
  360. /* Subroutines for re_compile_pattern.  */
  361. static void store_jump (), insert_jump (), store_jump_n (),
  362.         insert_jump_n (), insert_op_2 ();
  363.  
  364.  
  365. /* re_compile_pattern takes a regular-expression string
  366.    and converts it into a buffer full of byte commands for matching.
  367.  
  368.    PATTERN   is the address of the pattern string
  369.    SIZE      is the length of it.
  370.    BUFP        is a  struct re_pattern_buffer *  which points to the info
  371.          on where to store the byte commands.
  372.          This structure contains a  char *  which points to the
  373.          actual space, which should have been obtained with malloc.
  374.          re_compile_pattern may use realloc to grow the buffer space.
  375.  
  376.    The number of bytes of commands can be found out by looking in
  377.    the `struct re_pattern_buffer' that bufp pointed to, after
  378.    re_compile_pattern returns. */
  379.  
  380. char *
  381. re_compile_pattern (pattern, size, bufp)
  382.      char *pattern;
  383.      int size;
  384.      struct re_pattern_buffer *bufp;
  385. {
  386.   register char *b = bufp->buffer;
  387.   register char *p = pattern;
  388.   char *pend = pattern + size;
  389.   register unsigned c, c1;
  390.   char *p1;
  391.   unsigned char *translate = (unsigned char *) bufp->translate;
  392.  
  393.   /* Address of the count-byte of the most recently inserted `exactn'
  394.      command.  This makes it possible to tell whether a new exact-match
  395.      character can be added to that command or requires a new `exactn'
  396.      command.  */
  397.      
  398.   char *pending_exact = 0;
  399.  
  400.   /* Address of the place where a forward-jump should go to the end of
  401.      the containing expression.  Each alternative of an `or', except the
  402.      last, ends with a forward-jump of this sort.  */
  403.  
  404.   char *fixup_jump = 0;
  405.  
  406.   /* Address of start of the most recently finished expression.
  407.      This tells postfix * where to find the start of its operand.  */
  408.  
  409.   char *laststart = 0;
  410.  
  411.   /* In processing a repeat, 1 means zero matches is allowed.  */
  412.  
  413.   char zero_times_ok;
  414.  
  415.   /* In processing a repeat, 1 means many matches is allowed.  */
  416.  
  417.   char many_times_ok;
  418.  
  419.   /* Address of beginning of regexp, or inside of last \(.  */
  420.  
  421.   char *begalt = b;
  422.  
  423.   /* In processing an interval, at least this many matches must be made.  */
  424.   int lower_bound;
  425.  
  426.   /* In processing an interval, at most this many matches can be made.  */
  427.   int upper_bound;
  428.  
  429.   /* Place in pattern (i.e., the {) to which to go back if the interval
  430.      is invalid.  */
  431.   char *beg_interval = 0;
  432.   
  433.   /* Stack of information saved by \( and restored by \).
  434.      Four stack elements are pushed by each \(:
  435.        First, the value of b.
  436.        Second, the value of fixup_jump.
  437.        Third, the value of regnum.
  438.        Fourth, the value of begalt.  */
  439.  
  440.   int stackb[40];
  441.   int *stackp = stackb;
  442.   int *stacke = stackb + 40;
  443.   int *stackt;
  444.  
  445.   /* Counts \('s as they are encountered.  Remembered for the matching \),
  446.      where it becomes the register number to put in the stop_memory
  447.      command.  */
  448.  
  449.   int regnum = 1;
  450.  
  451.   bufp->fastmap_accurate = 0;
  452.  
  453. #ifndef emacs
  454. #ifndef SYNTAX_TABLE
  455.   /* Initialize the syntax table.  */
  456.    init_syntax_once();
  457. #endif
  458. #endif
  459.  
  460.   if (bufp->allocated == 0)
  461.     {
  462.       bufp->allocated = INIT_BUF_SIZE;
  463.       if (bufp->buffer)
  464.     /* EXTEND_BUFFER loses when bufp->allocated is 0.  */
  465.     bufp->buffer = (char *) realloc (bufp->buffer, INIT_BUF_SIZE);
  466.       else
  467.     /* Caller did not allocate a buffer.  Do it for them.  */
  468.     bufp->buffer = (char *) malloc (INIT_BUF_SIZE);
  469.       if (!bufp->buffer) goto memory_exhausted;
  470.       begalt = b = bufp->buffer;
  471.     }
  472.  
  473.   while (p != pend)
  474.     {
  475.       PATFETCH (c);
  476.  
  477.       switch (c)
  478.     {
  479.     case '$':
  480.       {
  481.         char *p1 = p;
  482.         /* When testing what follows the $,
  483.            look past the \-constructs that don't consume anything.  */
  484.         if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  485.           while (p1 != pend)
  486.         {
  487.           if (*p1 == '\\' && p1 + 1 != pend
  488.               && (p1[1] == '<' || p1[1] == '>'
  489.               || p1[1] == '`' || p1[1] == '\''
  490. #ifdef emacs
  491.               || p1[1] == '='
  492. #endif
  493.               || p1[1] == 'b' || p1[1] == 'B'))
  494.             p1 += 2;
  495.           else
  496.             break;
  497.         }
  498.             if (obscure_syntax & RE_TIGHT_VBAR)
  499.           {
  500.         if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS) && p1 != pend)
  501.           goto normal_char;
  502.         /* Make operand of last vbar end before this `$'.  */
  503.         if (fixup_jump)
  504.           store_jump (fixup_jump, jump, b);
  505.         fixup_jump = 0;
  506.         BUFPUSH (endline);
  507.         break;
  508.           }
  509.         /* $ means succeed if at end of line, but only in special contexts.
  510.           If validly in the middle of a pattern, it is a normal character. */
  511.  
  512.             if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS) && p1 != pend)
  513.           goto invalid_pattern;
  514.         if (p1 == pend || *p1 == '\n'
  515.         || (obscure_syntax & RE_CONTEXT_INDEP_OPS)
  516.         || (obscure_syntax & RE_NO_BK_PARENS
  517.             ? *p1 == ')'
  518.             : *p1 == '\\' && p1[1] == ')')
  519.         || (obscure_syntax & RE_NO_BK_VBAR
  520.             ? *p1 == '|'
  521.             : *p1 == '\\' && p1[1] == '|'))
  522.           {
  523.         BUFPUSH (endline);
  524.         break;
  525.           }
  526.         goto normal_char;
  527.           }
  528.     case '^':
  529.       /* ^ means succeed if at beg of line, but only if no preceding 
  530.              pattern.  */
  531.              
  532.           if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS) && laststart)
  533.             goto invalid_pattern;
  534.           if (laststart && p - 2 >= pattern && p[-2] != '\n'
  535.            && !(obscure_syntax & RE_CONTEXT_INDEP_OPS))
  536.         goto normal_char;
  537.       if (obscure_syntax & RE_TIGHT_VBAR)
  538.         {
  539.           if (p != pattern + 1
  540.           && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  541.         goto normal_char;
  542.           BUFPUSH (begline);
  543.           begalt = b;
  544.         }
  545.       else
  546.         BUFPUSH (begline);
  547.       break;
  548.  
  549.     case '+':
  550.     case '?':
  551.       if ((obscure_syntax & RE_BK_PLUS_QM)
  552.           || (obscure_syntax & RE_LIMITED_OPS))
  553.         goto normal_char;
  554.     handle_plus:
  555.     case '*':
  556.       /* If there is no previous pattern, char not special. */
  557.       if (!laststart)
  558.             {
  559.               if (obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  560.                 goto invalid_pattern;
  561.               else if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  562.         goto normal_char;
  563.             }
  564.       /* If there is a sequence of repetition chars,
  565.          collapse it down to just one.  */
  566.       zero_times_ok = 0;
  567.       many_times_ok = 0;
  568.       while (1)
  569.         {
  570.           zero_times_ok |= c != '+';
  571.           many_times_ok |= c != '?';
  572.           if (p == pend)
  573.         break;
  574.           PATFETCH (c);
  575.           if (c == '*')
  576.         ;
  577.           else if (!(obscure_syntax & RE_BK_PLUS_QM)
  578.                && (c == '+' || c == '?'))
  579.         ;
  580.           else if ((obscure_syntax & RE_BK_PLUS_QM)
  581.                && c == '\\')
  582.         {
  583.           int c1;
  584.           PATFETCH (c1);
  585.           if (!(c1 == '+' || c1 == '?'))
  586.             {
  587.               PATUNFETCH;
  588.               PATUNFETCH;
  589.               break;
  590.             }
  591.           c = c1;
  592.         }
  593.           else
  594.         {
  595.           PATUNFETCH;
  596.           break;
  597.         }
  598.         }
  599.  
  600.       /* Star, etc. applied to an empty pattern is equivalent
  601.          to an empty pattern.  */
  602.       if (!laststart)  
  603.         break;
  604.  
  605.       /* Now we know whether or not zero matches is allowed
  606.          and also whether or not two or more matches is allowed.  */
  607.       if (many_times_ok)
  608.         {
  609.           /* If more than one repetition is allowed, put in at the
  610.                  end a backward relative jump from b to before the next
  611.                  jump we're going to put in below (which jumps from
  612.                  laststart to after this jump).  */
  613.               GET_BUFFER_SPACE (3);
  614.           store_jump (b, maybe_finalize_jump, laststart - 3);
  615.           b += 3;      /* Because store_jump put stuff here.  */
  616.         }
  617.           /* On failure, jump from laststart to b + 3, which will be the
  618.              end of the buffer after this jump is inserted.  */
  619.           GET_BUFFER_SPACE (3);
  620.       insert_jump (on_failure_jump, laststart, b + 3, b);
  621.       pending_exact = 0;
  622.       b += 3;
  623.       if (!zero_times_ok)
  624.         {
  625.           /* At least one repetition is required, so insert a
  626.                  dummy-failure before the initial on-failure-jump
  627.                  instruction of the loop. This effects a skip over that
  628.                  instruction the first time we hit that loop.  */
  629.               GET_BUFFER_SPACE (6);
  630.               insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  631.           b += 3;
  632.         }
  633.       break;
  634.  
  635.     case '.':
  636.       laststart = b;
  637.       BUFPUSH (anychar);
  638.       break;
  639.  
  640.         case '[':
  641.           if (p == pend)
  642.             goto invalid_pattern;
  643.       while (b - bufp->buffer
  644.          > bufp->allocated - 3 - (1 << BYTEWIDTH) / BYTEWIDTH)
  645.         EXTEND_BUFFER;
  646.  
  647.       laststart = b;
  648.       if (*p == '^')
  649.         {
  650.               BUFPUSH (charset_not); 
  651.               p++;
  652.             }
  653.       else
  654.         BUFPUSH (charset);
  655.       p1 = p;
  656.  
  657.       BUFPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  658.       /* Clear the whole map */
  659.       bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  660.           
  661.       if ((obscure_syntax & RE_HAT_NOT_NEWLINE) && b[-2] == charset_not)
  662.             SET_LIST_BIT ('\n');
  663.  
  664.  
  665.       /* Read in characters and ranges, setting map bits.  */
  666.       while (1)
  667.         {
  668.           /* Don't translate while fetching, in case it's a range bound.
  669.          When we set the bit for the character, we translate it.  */
  670.           PATFETCH_RAW (c);
  671.  
  672.           /* If set, \ escapes characters when inside [...].  */
  673.           if ((obscure_syntax & RE_AWK_CLASS_HACK) && c == '\\')
  674.             {
  675.               PATFETCH(c1);
  676.                   SET_LIST_BIT (c1);
  677.               continue;
  678.             }
  679.               if (c == ']')
  680.                 {
  681.                   if (p == p1 + 1)
  682.                     {
  683.               /* If this is an empty bracket expression.  */
  684.                       if ((obscure_syntax & RE_NO_EMPTY_BRACKETS) 
  685.                           && p == pend)
  686.                         goto invalid_pattern;
  687.                     }
  688.                   else 
  689.             /* Stop if this isn't merely a ] inside a bracket
  690.                        expression, but rather the end of a bracket
  691.                        expression.  */
  692.                     break;
  693.                 }
  694.               /* Get a range.  */
  695.               if (p[0] == '-' && p[1] != ']')
  696.         {
  697.                   PATFETCH (c1);
  698.           /* Don't translate the range bounds while fetching them.  */
  699.           PATFETCH_RAW (c1);
  700.                   
  701.           if ((obscure_syntax & RE_NO_EMPTY_RANGES) && c > c1)
  702.                     goto invalid_pattern;
  703.                     
  704.           if ((obscure_syntax & RE_NO_HYPHEN_RANGE_END) 
  705.                       && c1 == '-' && *p != ']')
  706.                     goto invalid_pattern;
  707.                     
  708.                   while (c <= c1)
  709.             {
  710.               /* Translate each char that's in the range.  */
  711.               if (translate)
  712.             SET_LIST_BIT (translate[c]);
  713.               else
  714.             SET_LIST_BIT (c);
  715.                       c++;
  716.             }
  717.                 }
  718.           else if ((obscure_syntax & RE_CHAR_CLASSES)
  719.             &&  c == '[' && p[0] == ':')
  720.                 {
  721.           /* Longest valid character class word has six characters.  */
  722.                   char str[CHAR_CLASS_MAX_LENGTH];
  723.           PATFETCH (c);
  724.           c1 = 0;
  725.           /* If no ] at end.  */
  726.                   if (p == pend)
  727.                     goto invalid_pattern;
  728.           while (1)
  729.             {
  730.               /* Don't translate the ``character class'' characters.  */
  731.                       PATFETCH_RAW (c);
  732.               if (c == ':' || c == ']' || p == pend
  733.                           || c1 == CHAR_CLASS_MAX_LENGTH)
  734.                 break;
  735.               str[c1++] = c;
  736.             }
  737.           str[c1] = '\0';
  738.           if (p == pend     
  739.               || c == ']'    /* End of the bracket expression.  */
  740.                       || p[0] != ']'
  741.               || p + 1 == pend
  742.                       || (strcmp (str, "alpha") != 0 
  743.                           && strcmp (str, "upper") != 0
  744.               && strcmp (str, "lower") != 0 
  745.                           && strcmp (str, "digit") != 0
  746.               && strcmp (str, "alnum") != 0 
  747.                           && strcmp (str, "xdigit") != 0
  748.               && strcmp (str, "space") != 0 
  749.                           && strcmp (str, "print") != 0
  750.               && strcmp (str, "punct") != 0 
  751.                           && strcmp (str, "graph") != 0
  752.               && strcmp (str, "cntrl") != 0))
  753.             {
  754.                /* Undo the ending character, the letters, and leave 
  755.                           the leading : and [ (but set bits for them).  */
  756.                       c1++;
  757.               while (c1--)    
  758.             PATUNFETCH;
  759.               SET_LIST_BIT ('[');
  760.               SET_LIST_BIT (':');
  761.                 }
  762.                   else
  763.                     {
  764.                       /* The ] at the end of the character class.  */
  765.                       PATFETCH (c);                    
  766.                       if (c != ']')
  767.                         goto invalid_pattern;
  768.               for (c = 0; c < (1 << BYTEWIDTH); c++)
  769.             {
  770.               if ((strcmp (str, "alpha") == 0  && isalpha (c))
  771.                    || (strcmp (str, "upper") == 0  && isupper (c))
  772.                    || (strcmp (str, "lower") == 0  && islower (c))
  773.                    || (strcmp (str, "digit") == 0  && isdigit (c))
  774.                    || (strcmp (str, "alnum") == 0  && isalnum (c))
  775.                    || (strcmp (str, "xdigit") == 0  && isxdigit (c))
  776.                    || (strcmp (str, "space") == 0  && isspace (c))
  777.                    || (strcmp (str, "print") == 0  && isprint (c))
  778.                    || (strcmp (str, "punct") == 0  && ispunct (c))
  779.                    || (strcmp (str, "graph") == 0  && isgraph (c))
  780.                    || (strcmp (str, "cntrl") == 0  && iscntrl (c)))
  781.                 SET_LIST_BIT (c);
  782.             }
  783.             }
  784.                 }
  785.               else if (translate)
  786.         SET_LIST_BIT (translate[c]);
  787.           else
  788.                 SET_LIST_BIT (c);
  789.         }
  790.  
  791.           /* Discard any character set/class bitmap bytes that are all
  792.              0 at the end of the map. Decrement the map-length byte too.  */
  793.           while ((int) b[-1] > 0 && b[b[-1] - 1] == 0) 
  794.             b[-1]--; 
  795.           b += b[-1];
  796.           break;
  797.  
  798.     case '(':
  799.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  800.         goto normal_char;
  801.       else
  802.         goto handle_open;
  803.  
  804.     case ')':
  805.       if (! (obscure_syntax & RE_NO_BK_PARENS))
  806.         goto normal_char;
  807.       else
  808.         goto handle_close;
  809.  
  810.         case '\n':
  811.       if (! (obscure_syntax & RE_NEWLINE_OR))
  812.         goto normal_char;
  813.       else
  814.         goto handle_bar;
  815.  
  816.     case '|':
  817.       if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  818.               && (! laststart  ||  p == pend))
  819.         goto invalid_pattern;
  820.           else if (! (obscure_syntax & RE_NO_BK_VBAR))
  821.         goto normal_char;
  822.       else
  823.         goto handle_bar;
  824.  
  825.     case '{':
  826.            if (! ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  827.                   && (obscure_syntax & RE_INTERVALS)))
  828.              goto normal_char;
  829.            else
  830.              goto handle_interval;
  831.              
  832.         case '\\':
  833.       if (p == pend) goto invalid_pattern;
  834.       PATFETCH_RAW (c);
  835.       switch (c)
  836.         {
  837.         case '(':
  838.           if (obscure_syntax & RE_NO_BK_PARENS)
  839.         goto normal_backsl;
  840.         handle_open:
  841.           if (stackp == stacke) goto nesting_too_deep;
  842.  
  843.               /* Laststart should point to the start_memory that we are about
  844.                  to push (unless the pattern has RE_NREGS or more ('s).  */
  845.               *stackp++ = b - bufp->buffer;    
  846.           if (regnum < RE_NREGS)
  847.             {
  848.           BUFPUSH (start_memory);
  849.           BUFPUSH (regnum);
  850.             }
  851.           *stackp++ = fixup_jump ? fixup_jump - bufp->buffer + 1 : 0;
  852.           *stackp++ = regnum++;
  853.           *stackp++ = begalt - bufp->buffer;
  854.           fixup_jump = 0;
  855.           laststart = 0;
  856.           begalt = b;
  857.           break;
  858.  
  859.         case ')':
  860.           if (obscure_syntax & RE_NO_BK_PARENS)
  861.         goto normal_backsl;
  862.         handle_close:
  863.           if (stackp == stackb) goto unmatched_close;
  864.           begalt = *--stackp + bufp->buffer;
  865.           if (fixup_jump)
  866.         store_jump (fixup_jump, jump, b);
  867.           if (stackp[-1] < RE_NREGS)
  868.         {
  869.           BUFPUSH (stop_memory);
  870.           BUFPUSH (stackp[-1]);
  871.         }
  872.           stackp -= 2;
  873.               fixup_jump = *stackp ? *stackp + bufp->buffer - 1 : 0;
  874.               laststart = *--stackp + bufp->buffer;
  875.           break;
  876.  
  877.         case '|':
  878.               if ((obscure_syntax & RE_LIMITED_OPS)
  879.               || (obscure_syntax & RE_NO_BK_VBAR))
  880.         goto normal_backsl;
  881.         handle_bar:
  882.               if (obscure_syntax & RE_LIMITED_OPS)
  883.                 goto normal_char;
  884.           /* Insert before the previous alternative a jump which
  885.                  jumps to this alternative if the former fails.  */
  886.               GET_BUFFER_SPACE (6);
  887.               insert_jump (on_failure_jump, begalt, b + 6, b);
  888.           pending_exact = 0;
  889.           b += 3;
  890.           /* The alternative before the previous alternative has a
  891.                  jump after it which gets executed if it gets matched.
  892.                  Adjust that jump so it will jump to the previous
  893.                  alternative's analogous jump (put in below, which in
  894.                  turn will jump to the next (if any) alternative's such
  895.                  jump, etc.).  The last such jump jumps to the correct
  896.                  final destination.  */
  897.               if (fixup_jump)
  898.         store_jump (fixup_jump, jump, b);
  899.                 
  900.           /* Leave space for a jump after previous alternative---to be 
  901.                  filled in later.  */
  902.               fixup_jump = b;
  903.               b += 3;
  904.  
  905.               laststart = 0;
  906.           begalt = b;
  907.           break;
  908.  
  909.             case '{': 
  910.               if (! (obscure_syntax & RE_INTERVALS)
  911.           /* Let \{ be a literal.  */
  912.                   || ((obscure_syntax & RE_INTERVALS)
  913.                       && (obscure_syntax & RE_NO_BK_CURLY_BRACES))
  914.           /* If it's the string "\{".  */
  915.           || (p - 2 == pattern  &&  p == pend))
  916.                 goto normal_backsl;
  917.             handle_interval:
  918.           beg_interval = p - 1;        /* The {.  */
  919.               /* If there is no previous pattern, this isn't an interval.  */
  920.           if (!laststart)
  921.             {
  922.                   if (obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  923.             goto invalid_pattern;
  924.                   else
  925.                     goto normal_backsl;
  926.                 }
  927.               /* It also isn't an interval if not preceded by an re
  928.                  matching a single character or subexpression, or if
  929.                  the current type of intervals can't handle back
  930.                  references and the previous thing is a back reference.  */
  931.               if (! (*laststart == anychar
  932.              || *laststart == charset
  933.              || *laststart == charset_not
  934.              || *laststart == start_memory
  935.              || (*laststart == exactn  &&  laststart[1] == 1)
  936.              || (! (obscure_syntax & RE_NO_BK_REFS)
  937.                          && *laststart == duplicate)))
  938.                 {
  939.                   if (obscure_syntax & RE_NO_BK_CURLY_BRACES)
  940.                     goto normal_char;
  941.                     
  942.           /* Posix extended syntax is handled in previous
  943.                      statement; this is for Posix basic syntax.  */
  944.                   if (obscure_syntax & RE_INTERVALS)
  945.                     goto invalid_pattern;
  946.                     
  947.                   goto normal_backsl;
  948.         }
  949.               lower_bound = -1;            /* So can see if are set.  */
  950.           upper_bound = -1;
  951.               GET_UNSIGNED_NUMBER (lower_bound);
  952.           if (c == ',')
  953.         {
  954.           GET_UNSIGNED_NUMBER (upper_bound);
  955.           if (upper_bound < 0)
  956.             upper_bound = RE_DUP_MAX;
  957.         }
  958.           if (upper_bound < 0)
  959.         upper_bound = lower_bound;
  960.               if (! (obscure_syntax & RE_NO_BK_CURLY_BRACES)) 
  961.                 {
  962.                   if (c != '\\')
  963.                     goto invalid_pattern;
  964.                   PATFETCH (c);
  965.                 }
  966.           if (c != '}' || lower_bound < 0 || upper_bound > RE_DUP_MAX
  967.           || lower_bound > upper_bound 
  968.                   || ((obscure_syntax & RE_NO_BK_CURLY_BRACES) 
  969.               && p != pend  && *p == '{')) 
  970.             {
  971.           if (obscure_syntax & RE_NO_BK_CURLY_BRACES)
  972.                     goto unfetch_interval;
  973.                   else
  974.                     goto invalid_pattern;
  975.         }
  976.  
  977.           /* If upper_bound is zero, don't want to succeed at all; 
  978.           jump from laststart to b + 3, which will be the end of
  979.                  the buffer after this jump is inserted.  */
  980.                  
  981.                if (upper_bound == 0)
  982.                  {
  983.                    GET_BUFFER_SPACE (3);
  984.                    insert_jump (jump, laststart, b + 3, b);
  985.                    b += 3;
  986.                  }
  987.  
  988.                /* Otherwise, after lower_bound number of succeeds, jump
  989.                   to after the jump_n which will be inserted at the end
  990.                   of the buffer, and insert that jump_n.  */
  991.                else 
  992.          { /* Set to 5 if only one repetition is allowed and
  993.                   hence no jump_n is inserted at the current end of
  994.                       the buffer; then only space for the succeed_n is
  995.                       needed.  Otherwise, need space for both the
  996.                       succeed_n and the jump_n.  */
  997.                       
  998.                    unsigned slots_needed = upper_bound == 1 ? 5 : 10;
  999.                      
  1000.                    GET_BUFFER_SPACE (slots_needed);
  1001.                    /* Initialize the succeed_n to n, even though it will
  1002.                       be set by its attendant set_number_at, because
  1003.                       re_compile_fastmap will need to know it.  Jump to
  1004.                       what the end of buffer will be after inserting
  1005.                       this succeed_n and possibly appending a jump_n.  */
  1006.                    insert_jump_n (succeed_n, laststart, b + slots_needed, 
  1007.                           b, lower_bound);
  1008.                    b += 5;     /* Just increment for the succeed_n here.  */
  1009.  
  1010.           /* More than one repetition is allowed, so put in at
  1011.              the end of the buffer a backward jump from b to the
  1012.                      succeed_n we put in above.  By the time we've gotten
  1013.                      to this jump when matching, we'll have matched once
  1014.                      already, so jump back only upper_bound - 1 times.  */
  1015.  
  1016.                    if (upper_bound > 1)
  1017.                      {
  1018.                        store_jump_n (b, jump_n, laststart, upper_bound - 1);
  1019.                        b += 5;
  1020.                        /* When hit this when matching, reset the
  1021.                           preceding jump_n's n to upper_bound - 1.  */
  1022.                        BUFPUSH (set_number_at);
  1023.                GET_BUFFER_SPACE (2);
  1024.                        STORE_NUMBER_AND_INCR (b, -5);
  1025.                        STORE_NUMBER_AND_INCR (b, upper_bound - 1);
  1026.                      }
  1027.            /* When hit this when matching, set the succeed_n's n.  */
  1028.                    GET_BUFFER_SPACE (5);
  1029.            insert_op_2 (set_number_at, laststart, b, 5, lower_bound);
  1030.                    b += 5;
  1031.                  }
  1032.               pending_exact = 0;
  1033.           beg_interval = 0;
  1034.               break;
  1035.  
  1036.  
  1037.             unfetch_interval:
  1038.           /* If an invalid interval, match the characters as literals.  */
  1039.            if (beg_interval)
  1040.                  p = beg_interval;
  1041.              else
  1042.                  {
  1043.                    fprintf (stderr, 
  1044.               "regex: no interval beginning to which to backtrack.\n");
  1045.            exit (1);
  1046.                  }
  1047.                  
  1048.                beg_interval = 0;
  1049.                PATFETCH (c);        /* normal_char expects char in `c'.  */
  1050.            goto normal_char;
  1051.            break;
  1052.  
  1053. #ifdef emacs
  1054.         case '=':
  1055.           BUFPUSH (at_dot);
  1056.           break;
  1057.  
  1058.         case 's':    
  1059.           laststart = b;
  1060.           BUFPUSH (syntaxspec);
  1061.           PATFETCH (c);
  1062.           BUFPUSH (syntax_spec_code[c]);
  1063.           break;
  1064.  
  1065.         case 'S':
  1066.           laststart = b;
  1067.           BUFPUSH (notsyntaxspec);
  1068.           PATFETCH (c);
  1069.           BUFPUSH (syntax_spec_code[c]);
  1070.           break;
  1071. #endif /* emacs */
  1072.  
  1073.         case 'w':
  1074.           laststart = b;
  1075.           BUFPUSH (wordchar);
  1076.           break;
  1077.  
  1078.         case 'W':
  1079.           laststart = b;
  1080.           BUFPUSH (notwordchar);
  1081.           break;
  1082.  
  1083.         case '<':
  1084.           BUFPUSH (wordbeg);
  1085.           break;
  1086.  
  1087.         case '>':
  1088.           BUFPUSH (wordend);
  1089.           break;
  1090.  
  1091.         case 'b':
  1092.           BUFPUSH (wordbound);
  1093.           break;
  1094.  
  1095.         case 'B':
  1096.           BUFPUSH (notwordbound);
  1097.           break;
  1098.  
  1099.         case '`':
  1100.           BUFPUSH (begbuf);
  1101.           break;
  1102.  
  1103.         case '\'':
  1104.           BUFPUSH (endbuf);
  1105.           break;
  1106.  
  1107.         case '1':
  1108.         case '2':
  1109.         case '3':
  1110.         case '4':
  1111.         case '5':
  1112.         case '6':
  1113.         case '7':
  1114.         case '8':
  1115.         case '9':
  1116.           if (obscure_syntax & RE_NO_BK_REFS)
  1117.                 goto normal_char;
  1118.               c1 = c - '0';
  1119.           if (c1 >= regnum)
  1120.         {
  1121.             if (obscure_syntax & RE_NO_EMPTY_BK_REF)
  1122.                     goto invalid_pattern;
  1123.                   else
  1124.                     goto normal_char;
  1125.                 }
  1126.               /* Can't back reference to a subexpression if inside of it.  */
  1127.               for (stackt = stackp - 2;  stackt > stackb;  stackt -= 4)
  1128.          if (*stackt == c1)
  1129.           goto normal_char;
  1130.           laststart = b;
  1131.           BUFPUSH (duplicate);
  1132.           BUFPUSH (c1);
  1133.           break;
  1134.  
  1135.         case '+':
  1136.         case '?':
  1137.           if (obscure_syntax & RE_BK_PLUS_QM)
  1138.         goto handle_plus;
  1139.           else
  1140.                 goto normal_backsl;
  1141.               break;
  1142.  
  1143.             default:
  1144.         normal_backsl:
  1145.           /* You might think it would be useful for \ to mean
  1146.          not to translate; but if we don't translate it
  1147.          it will never match anything.  */
  1148.           if (translate) c = translate[c];
  1149.           goto normal_char;
  1150.         }
  1151.       break;
  1152.  
  1153.     default:
  1154.     normal_char:        /* Expects the character in `c'.  */
  1155.       if (!pending_exact || pending_exact + *pending_exact + 1 != b
  1156.           || *pending_exact == 0177 || *p == '*' || *p == '^'
  1157.           || ((obscure_syntax & RE_BK_PLUS_QM)
  1158.           ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  1159.           : (*p == '+' || *p == '?'))
  1160.           || ((obscure_syntax & RE_INTERVALS) 
  1161.                   && ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  1162.               ? *p == '{'
  1163.                       : (p[0] == '\\' && p[1] == '{'))))
  1164.         {
  1165.           laststart = b;
  1166.           BUFPUSH (exactn);
  1167.           pending_exact = b;
  1168.           BUFPUSH (0);
  1169.         }
  1170.       BUFPUSH (c);
  1171.       (*pending_exact)++;
  1172.     }
  1173.     }
  1174.  
  1175.   if (fixup_jump)
  1176.     store_jump (fixup_jump, jump, b);
  1177.  
  1178.   if (stackp != stackb) goto unmatched_open;
  1179.  
  1180.   bufp->used = b - bufp->buffer;
  1181.   return 0;
  1182.  
  1183.  invalid_pattern:
  1184.   return "Invalid regular expression";
  1185.  
  1186.  unmatched_open:
  1187.   return "Unmatched \\(";
  1188.  
  1189.  unmatched_close:
  1190.   return "Unmatched \\)";
  1191.  
  1192.  end_of_pattern:
  1193.   return "Premature end of regular expression";
  1194.  
  1195.  nesting_too_deep:
  1196.   return "Nesting too deep";
  1197.  
  1198.  too_big:
  1199.   return "Regular expression too big";
  1200.  
  1201.  memory_exhausted:
  1202.   return "Memory exhausted";
  1203. }
  1204.  
  1205.  
  1206. /* Store a jump of the form <OPCODE> <relative address>.
  1207.    Store in the location FROM a jump operation to jump to relative
  1208.    address FROM - TO.  OPCODE is the opcode to store.  */
  1209.  
  1210. static void
  1211. store_jump (from, opcode, to)
  1212.      char *from, *to;
  1213.      char opcode;
  1214. {
  1215.   from[0] = opcode;
  1216.   STORE_NUMBER(from + 1, to - (from + 3));
  1217. }
  1218.  
  1219.  
  1220. /* Open up space before char FROM, and insert there a jump to TO.
  1221.    CURRENT_END gives the end of the storage not in use, so we know 
  1222.    how much data to copy up. OP is the opcode of the jump to insert.
  1223.  
  1224.    If you call this function, you must zero out pending_exact.  */
  1225.  
  1226. static void
  1227. insert_jump (op, from, to, current_end)
  1228.      char op;
  1229.      char *from, *to, *current_end;
  1230. {
  1231.   register char *pfrom = current_end;        /* Copy from here...  */
  1232.   register char *pto = current_end + 3;        /* ...to here.  */
  1233.  
  1234.   while (pfrom != from)                   
  1235.     *--pto = *--pfrom;
  1236.   store_jump (from, op, to);
  1237. }
  1238.  
  1239.  
  1240. /* Store a jump of the form <opcode> <relative address> <n> .
  1241.  
  1242.    Store in the location FROM a jump operation to jump to relative
  1243.    address FROM - TO.  OPCODE is the opcode to store, N is a number the
  1244.    jump uses, say, to decide how many times to jump.
  1245.    
  1246.    If you call this function, you must zero out pending_exact.  */
  1247.  
  1248. static void
  1249. store_jump_n (from, opcode, to, n)
  1250.      char *from, *to;
  1251.      char opcode;
  1252.      unsigned n;
  1253. {
  1254.   from[0] = opcode;
  1255.   STORE_NUMBER (from + 1, to - (from + 3));
  1256.   STORE_NUMBER (from + 3, n);
  1257. }
  1258.  
  1259.  
  1260. /* Similar to insert_jump, but handles a jump which needs an extra
  1261.    number to handle minimum and maximum cases.  Open up space at
  1262.    location FROM, and insert there a jump to TO.  CURRENT_END gives the
  1263.    end of the storage in use, so we know how much data to copy up. OP is
  1264.    the opcode of the jump to insert.
  1265.  
  1266.    If you call this function, you must zero out pending_exact.  */
  1267.  
  1268. static void
  1269. insert_jump_n (op, from, to, current_end, n)
  1270.      char op;
  1271.      char *from, *to, *current_end;
  1272.      unsigned n;
  1273. {
  1274.   register char *pfrom = current_end;        /* Copy from here...  */
  1275.   register char *pto = current_end + 5;        /* ...to here.  */
  1276.  
  1277.   while (pfrom != from)                   
  1278.     *--pto = *--pfrom;
  1279.   store_jump_n (from, op, to, n);
  1280. }
  1281.  
  1282.  
  1283. /* Open up space at location THERE, and insert operation OP followed by
  1284.    NUM_1 and NUM_2.  CURRENT_END gives the end of the storage in use, so
  1285.    we know how much data to copy up.
  1286.  
  1287.    If you call this function, you must zero out pending_exact.  */
  1288.  
  1289. static void
  1290. insert_op_2 (op, there, current_end, num_1, num_2)
  1291.      char op;
  1292.      char *there, *current_end;
  1293.      int num_1, num_2;
  1294. {
  1295.   register char *pfrom = current_end;        /* Copy from here...  */
  1296.   register char *pto = current_end + 5;        /* ...to here.  */
  1297.  
  1298.   while (pfrom != there)                   
  1299.     *--pto = *--pfrom;
  1300.   
  1301.   there[0] = op;
  1302.   STORE_NUMBER (there + 1, num_1);
  1303.   STORE_NUMBER (there + 3, num_2);
  1304. }
  1305.  
  1306.  
  1307.  
  1308. /* Given a pattern, compute a fastmap from it.  The fastmap records
  1309.    which of the (1 << BYTEWIDTH) possible characters can start a string
  1310.    that matches the pattern.  This fastmap is used by re_search to skip
  1311.    quickly over totally implausible text.
  1312.  
  1313.    The caller must supply the address of a (1 << BYTEWIDTH)-byte data 
  1314.    area as bufp->fastmap.
  1315.    The other components of bufp describe the pattern to be used.  */
  1316.  
  1317. void
  1318. re_compile_fastmap (bufp)
  1319.      struct re_pattern_buffer *bufp;
  1320. {
  1321.   unsigned char *pattern = (unsigned char *) bufp->buffer;
  1322.   int size = bufp->used;
  1323.   register char *fastmap = bufp->fastmap;
  1324.   register unsigned char *p = pattern;
  1325.   register unsigned char *pend = pattern + size;
  1326.   register int j, k;
  1327.   unsigned char *translate = (unsigned char *) bufp->translate;
  1328.  
  1329.   unsigned char *stackb[NFAILURES];
  1330.   unsigned char **stackp = stackb;
  1331.  
  1332.   unsigned is_a_succeed_n;
  1333.  
  1334.   bzero (fastmap, (1 << BYTEWIDTH));
  1335.   bufp->fastmap_accurate = 1;
  1336.   bufp->can_be_null = 0;
  1337.       
  1338.   while (p)
  1339.     {
  1340.       is_a_succeed_n = 0;
  1341.       if (p == pend)
  1342.     {
  1343.       bufp->can_be_null = 1;
  1344.       break;
  1345.     }
  1346. #ifdef SWITCH_ENUM_BUG
  1347.       switch ((int) ((enum regexpcode) *p++))
  1348. #else
  1349.       switch ((enum regexpcode) *p++)
  1350. #endif
  1351.     {
  1352.     case exactn:
  1353.       if (translate)
  1354.         fastmap[translate[p[1]]] = 1;
  1355.       else
  1356.         fastmap[p[1]] = 1;
  1357.       break;
  1358.  
  1359.         case begline:
  1360.         case before_dot:
  1361.     case at_dot:
  1362.     case after_dot:
  1363.     case begbuf:
  1364.     case endbuf:
  1365.     case wordbound:
  1366.     case notwordbound:
  1367.     case wordbeg:
  1368.     case wordend:
  1369.           continue;
  1370.  
  1371.     case endline:
  1372.       if (translate)
  1373.         fastmap[translate['\n']] = 1;
  1374.       else
  1375.         fastmap['\n'] = 1;
  1376.             
  1377.       if (bufp->can_be_null != 1)
  1378.         bufp->can_be_null = 2;
  1379.       break;
  1380.  
  1381.     case jump_n:
  1382.         case finalize_jump:
  1383.     case maybe_finalize_jump:
  1384.     case jump:
  1385.     case dummy_failure_jump:
  1386.           EXTRACT_NUMBER_AND_INCR (j, p);
  1387.       p += j;    
  1388.       if (j > 0)
  1389.         continue;
  1390.           /* Jump backward reached implies we just went through
  1391.          the body of a loop and matched nothing.
  1392.          Opcode jumped to should be an on_failure_jump.
  1393.          Just treat it like an ordinary jump.
  1394.          For a * loop, it has pushed its failure point already;
  1395.          If so, discard that as redundant.  */
  1396.  
  1397.           if ((enum regexpcode) *p != on_failure_jump
  1398.           && (enum regexpcode) *p != succeed_n)
  1399.         continue;
  1400.           p++;
  1401.           EXTRACT_NUMBER_AND_INCR (j, p);
  1402.           p += j;        
  1403.           if (stackp != stackb && *stackp == p)
  1404.             stackp--;
  1405.           continue;
  1406.       
  1407.         case on_failure_jump:
  1408.     handle_on_failure_jump:
  1409.           EXTRACT_NUMBER_AND_INCR (j, p);
  1410.           *++stackp = p + j;
  1411.       if (is_a_succeed_n)
  1412.             EXTRACT_NUMBER_AND_INCR (k, p);    /* Skip the n.  */
  1413.       continue;
  1414.  
  1415.     case succeed_n:
  1416.       is_a_succeed_n = 1;
  1417.           /* Get to the number of times to succeed.  */
  1418.           p += 2;        
  1419.       /* Increment p past the n for when k != 0.  */
  1420.           EXTRACT_NUMBER_AND_INCR (k, p);
  1421.           if (k == 0)
  1422.         {
  1423.               p -= 4;
  1424.               goto handle_on_failure_jump;
  1425.             }
  1426.           continue;
  1427.           
  1428.     case set_number_at:
  1429.           p += 4;
  1430.           continue;
  1431.  
  1432.         case start_memory:
  1433.     case stop_memory:
  1434.       p++;
  1435.       continue;
  1436.  
  1437.     case duplicate:
  1438.       bufp->can_be_null = 1;
  1439.       fastmap['\n'] = 1;
  1440.     case anychar:
  1441.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1442.         if (j != '\n')
  1443.           fastmap[j] = 1;
  1444.       if (bufp->can_be_null)
  1445.         return;
  1446.       /* Don't return; check the alternative paths
  1447.          so we can set can_be_null if appropriate.  */
  1448.       break;
  1449.  
  1450.     case wordchar:
  1451.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1452.         if (SYNTAX (j) == Sword)
  1453.           fastmap[j] = 1;
  1454.       break;
  1455.  
  1456.     case notwordchar:
  1457.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1458.         if (SYNTAX (j) != Sword)
  1459.           fastmap[j] = 1;
  1460.       break;
  1461.  
  1462. #ifdef emacs
  1463.     case syntaxspec:
  1464.       k = *p++;
  1465.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1466.         if (SYNTAX (j) == (enum syntaxcode) k)
  1467.           fastmap[j] = 1;
  1468.       break;
  1469.  
  1470.     case notsyntaxspec:
  1471.       k = *p++;
  1472.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  1473.         if (SYNTAX (j) != (enum syntaxcode) k)
  1474.           fastmap[j] = 1;
  1475.       break;
  1476. #endif /* not emacs */
  1477.  
  1478.     case charset:
  1479.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1480.         if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  1481.           {
  1482.         if (translate)
  1483.           fastmap[translate[j]] = 1;
  1484.         else
  1485.           fastmap[j] = 1;
  1486.           }
  1487.       break;
  1488.  
  1489.     case charset_not:
  1490.       /* Chars beyond end of map must be allowed */
  1491.       for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  1492.         if (translate)
  1493.           fastmap[translate[j]] = 1;
  1494.         else
  1495.           fastmap[j] = 1;
  1496.  
  1497.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1498.         if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  1499.           {
  1500.         if (translate)
  1501.           fastmap[translate[j]] = 1;
  1502.         else
  1503.           fastmap[j] = 1;
  1504.           }
  1505.       break;
  1506.     }
  1507.  
  1508.       /* Get here means we have successfully found the possible starting
  1509.          characters of one path of the pattern.  We need not follow this
  1510.          path any farther.  Instead, look at the next alternative
  1511.          remembered in the stack.  */
  1512.    if (stackp != stackb)
  1513.     p = *stackp--;
  1514.       else
  1515.     break;
  1516.     }
  1517. }
  1518.  
  1519.  
  1520.  
  1521. /* Like re_search_2, below, but only one string is specified, and
  1522.    doesn't let you say where to stop matching. */
  1523.  
  1524. int
  1525. re_search (pbufp, string, size, startpos, range, regs)
  1526.      struct re_pattern_buffer *pbufp;
  1527.      char *string;
  1528.      int size, startpos, range;
  1529.      struct re_registers *regs;
  1530. {
  1531.   return re_search_2 (pbufp, (char *) 0, 0, string, size, startpos, range, 
  1532.               regs, size);
  1533. }
  1534.  
  1535.  
  1536. /* Using the compiled pattern in PBUFP->buffer, first tries to match the
  1537.    virtual concatenation of STRING1 and STRING2, starting first at index
  1538.    STARTPOS, then at STARTPOS + 1, and so on.  RANGE is the number of
  1539.    places to try before giving up.  If RANGE is negative, it searches
  1540.    backwards, i.e., the starting positions tried are STARTPOS, STARTPOS
  1541.    - 1, etc.  STRING1 and STRING2 are of SIZE1 and SIZE2, respectively.
  1542.    In REGS, return the indices of the virtual concatenation of STRING1
  1543.    and STRING2 that matched the entire PBUFP->buffer and its contained
  1544.    subexpressions.  Do not consider matching one past the index MSTOP in
  1545.    the virtual concatenation of STRING1 and STRING2.
  1546.  
  1547.    The value returned is the position in the strings at which the match
  1548.    was found, or -1 if no match was found, or -2 if error (such as
  1549.    failure stack overflow).  */
  1550.  
  1551. int
  1552. re_search_2 (pbufp, string1, size1, string2, size2, startpos, range,
  1553.          regs, mstop)
  1554.      struct re_pattern_buffer *pbufp;
  1555.      char *string1, *string2;
  1556.      int size1, size2;
  1557.      int startpos;
  1558.      register int range;
  1559.      struct re_registers *regs;
  1560.      int mstop;
  1561. {
  1562.   register char *fastmap = pbufp->fastmap;
  1563.   register unsigned char *translate = (unsigned char *) pbufp->translate;
  1564.   int total_size = size1 + size2;
  1565.   int endpos = startpos + range;
  1566.   int val;
  1567.  
  1568.   /* Check for out-of-range starting position.  */
  1569.   if (startpos < 0  ||  startpos > total_size)
  1570.     return -1;
  1571.     
  1572.   /* Fix up range if it would eventually take startpos outside of the
  1573.      virtual concatenation of string1 and string2.  */
  1574.   if (endpos < -1)
  1575.     range = -1 - startpos;
  1576.   else if (endpos > total_size)
  1577.     range = total_size - startpos;
  1578.  
  1579.   /* Update the fastmap now if not correct already.  */
  1580.   if (fastmap && !pbufp->fastmap_accurate)
  1581.     re_compile_fastmap (pbufp);
  1582.   
  1583.   /* If the search isn't to be a backwards one, don't waste time in a
  1584.      long search for a pattern that says it is anchored.  */
  1585.   if (pbufp->used > 0 && (enum regexpcode) pbufp->buffer[0] == begbuf
  1586.       && range > 0)
  1587.     {
  1588.       if (startpos > 0)
  1589.     return -1;
  1590.       else
  1591.     range = 1;
  1592.     }
  1593.  
  1594.   while (1)
  1595.     { 
  1596.       /* If a fastmap is supplied, skip quickly over characters that
  1597.          cannot possibly be the start of a match.  Note, however, that
  1598.          if the pattern can possibly match the null string, we must
  1599.          test it at each starting point so that we take the first null
  1600.          string we get.  */
  1601.  
  1602.       if (fastmap && startpos < total_size && pbufp->can_be_null != 1)
  1603.     {
  1604.       if (range > 0)    /* Searching forwards.  */
  1605.         {
  1606.           register int lim = 0;
  1607.           register unsigned char *p;
  1608.           int irange = range;
  1609.           if (startpos < size1 && startpos + range >= size1)
  1610.         lim = range - (size1 - startpos);
  1611.  
  1612.           p = ((unsigned char *)
  1613.            &(startpos >= size1 ? string2 - size1 : string1)[startpos]);
  1614.  
  1615.               while (range > lim && !fastmap[translate 
  1616.                                              ? translate[*p++]
  1617.                                              : *p++])
  1618.             range--;
  1619.           startpos += irange - range;
  1620.         }
  1621.       else                /* Searching backwards.  */
  1622.         {
  1623.           register unsigned char c;
  1624.  
  1625.               if (string1 == 0 || startpos >= size1)
  1626.         c = string2[startpos - size1];
  1627.           else 
  1628.         c = string1[startpos];
  1629.  
  1630.               c &= 0xff;
  1631.           if (translate ? !fastmap[translate[c]] : !fastmap[c])
  1632.         goto advance;
  1633.         }
  1634.     }
  1635.  
  1636.       if (range >= 0 && startpos == total_size
  1637.       && fastmap && pbufp->can_be_null == 0)
  1638.     return -1;
  1639.  
  1640.       val = re_match_2 (pbufp, string1, size1, string2, size2, startpos,
  1641.             regs, mstop);
  1642.       if (val >= 0)
  1643.     return startpos;
  1644.       if (val == -2)
  1645.     return -2;
  1646.  
  1647. #ifdef C_ALLOCA
  1648.       alloca (0);
  1649. #endif /* C_ALLOCA */
  1650.  
  1651.     advance:
  1652.       if (!range) 
  1653.         break;
  1654.       else if (range > 0) 
  1655.         {
  1656.           range--; 
  1657.           startpos++;
  1658.         }
  1659.       else
  1660.         {
  1661.           range++; 
  1662.           startpos--;
  1663.         }
  1664.     }
  1665.   return -1;
  1666. }
  1667.  
  1668.  
  1669.  
  1670. #ifndef emacs   /* emacs never uses this.  */
  1671. int
  1672. re_match (pbufp, string, size, pos, regs)
  1673.      struct re_pattern_buffer *pbufp;
  1674.      char *string;
  1675.      int size, pos;
  1676.      struct re_registers *regs;
  1677. {
  1678.   return re_match_2 (pbufp, (char *) 0, 0, string, size, pos, regs, size); 
  1679. }
  1680. #endif /* not emacs */
  1681.  
  1682.  
  1683. /* The following are used for re_match_2, defined below:  */
  1684.  
  1685. /* Roughly the maximum number of failure points on the stack.  Would be
  1686.    exactly that if always pushed MAX_NUM_FAILURE_ITEMS each time we failed.  */
  1687.    
  1688. int re_max_failures = 2000;
  1689.  
  1690. /* Routine used by re_match_2.  */
  1691. static int bcmp_translate ();
  1692.  
  1693.  
  1694. /* Structure and accessing macros used in re_match_2:  */
  1695.  
  1696. struct register_info
  1697. {
  1698.   unsigned is_active : 1;
  1699.   unsigned matched_something : 1;
  1700. };
  1701.  
  1702. #define IS_ACTIVE(R)  ((R).is_active)
  1703. #define MATCHED_SOMETHING(R)  ((R).matched_something)
  1704.  
  1705.  
  1706. /* Macros used by re_match_2:  */
  1707.  
  1708.  
  1709. /* I.e., regstart, regend, and reg_info.  */
  1710.  
  1711. #define NUM_REG_ITEMS  3
  1712.  
  1713. /* We push at most this many things on the stack whenever we
  1714.    fail.  The `+ 2' refers to PATTERN_PLACE and STRING_PLACE, which are
  1715.    arguments to the PUSH_FAILURE_POINT macro.  */
  1716.  
  1717. #define MAX_NUM_FAILURE_ITEMS   (RE_NREGS * NUM_REG_ITEMS + 2)
  1718.  
  1719.  
  1720. /* We push this many things on the stack whenever we fail.  */
  1721.  
  1722. #define NUM_FAILURE_ITEMS  (last_used_reg * NUM_REG_ITEMS + 2)
  1723.  
  1724.  
  1725. /* This pushes most of the information about the current state we will want
  1726.    if we ever fail back to it.  */
  1727.  
  1728. #define PUSH_FAILURE_POINT(pattern_place, string_place)            \
  1729.   {                                    \
  1730.     short last_used_reg, this_reg;                    \
  1731.                                     \
  1732.     /* Find out how many registers are active or have been matched.    \
  1733.        (Aside from register zero, which is only set at the end.)  */    \
  1734.     for (last_used_reg = RE_NREGS - 1; last_used_reg > 0; last_used_reg--)\
  1735.       if (regstart[last_used_reg] != (unsigned char *) -1)        \
  1736.         break;                                \
  1737.                                     \
  1738.     if (stacke - stackp < NUM_FAILURE_ITEMS)                \
  1739.       {                                    \
  1740.     unsigned char **stackx;                        \
  1741.     unsigned int len = stacke - stackb;                \
  1742.     if (len > re_max_failures * MAX_NUM_FAILURE_ITEMS)        \
  1743.       return -2;                            \
  1744.                                     \
  1745.         /* Roughly double the size of the stack.  */            \
  1746.         stackx = (unsigned char **) alloca (2 * len            \
  1747.                                             * sizeof (unsigned char *));\
  1748.     /* Only copy what is in use.  */                \
  1749.         bcopy (stackb, stackx, len * sizeof (char *));            \
  1750.     stackp = stackx + (stackp - stackb);                \
  1751.     stackb = stackx;                        \
  1752.     stacke = stackb + 2 * len;                    \
  1753.       }                                    \
  1754.                                     \
  1755.     /* Now push the info for each of those registers.  */        \
  1756.     for (this_reg = 1; this_reg <= last_used_reg; this_reg++)        \
  1757.       {                                    \
  1758.         *stackp++ = regstart[this_reg];                    \
  1759.         *stackp++ = regend[this_reg];                    \
  1760.         *stackp++ = (unsigned char *) ®_info[this_reg];        \
  1761.       }                                    \
  1762.                                     \
  1763.     /* Push how many registers we saved.  */                \
  1764.     *stackp++ = (unsigned char *) last_used_reg;            \
  1765.                                     \
  1766.     *stackp++ = pattern_place;                                          \
  1767.     *stackp++ = string_place;                                           \
  1768.   }
  1769.   
  1770.  
  1771. /* This pops what PUSH_FAILURE_POINT pushes.  */
  1772.  
  1773. #define POP_FAILURE_POINT()                        \
  1774.   {                                    \
  1775.     int temp;                                \
  1776.     stackp -= 2;        /* Remove failure points.  */        \
  1777.     temp = (int) *--stackp;    /* How many regs pushed.  */            \
  1778.     temp *= NUM_REG_ITEMS;    /* How much to take off the stack.  */    \
  1779.     stackp -= temp;         /* Remove the register info.  */    \
  1780.   }
  1781.  
  1782.  
  1783. #define MATCHING_IN_FIRST_STRING  (dend == end_match_1)
  1784.  
  1785. /* Is true if there is a first string and if PTR is pointing anywhere
  1786.    inside it or just past the end.  */
  1787.    
  1788. #define IS_IN_FIRST_STRING(ptr)                     \
  1789.     (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
  1790.  
  1791. /* Call before fetching a character with *d.  This switches over to
  1792.    string2 if necessary.  */
  1793.  
  1794. #define PREFETCH                            \
  1795.  while (d == dend)                                \
  1796.   {                                    \
  1797.     /* end of string2 => fail.  */                    \
  1798.     if (dend == end_match_2)                         \
  1799.       goto fail;                            \
  1800.     /* end of string1 => advance to string2.  */             \
  1801.     d = string2;                                \
  1802.     dend = end_match_2;                            \
  1803.   }
  1804.  
  1805.  
  1806. /* Call this when have matched something; it sets `matched' flags for the
  1807.    registers corresponding to the subexpressions of which we currently
  1808.    are inside.  */
  1809. #define SET_REGS_MATCHED                         \
  1810.   { unsigned this_reg;                             \
  1811.     for (this_reg = 0; this_reg < RE_NREGS; this_reg++)         \
  1812.       {                                 \
  1813.         if (IS_ACTIVE(reg_info[this_reg]))                \
  1814.           MATCHED_SOMETHING(reg_info[this_reg]) = 1;            \
  1815.         else                                \
  1816.           MATCHED_SOMETHING(reg_info[this_reg]) = 0;            \
  1817.       }                                 \
  1818.   }
  1819.  
  1820. /* Test if at very beginning or at very end of the virtual concatenation
  1821.    of string1 and string2.  If there is only one string, we've put it in
  1822.    string2.  */
  1823.  
  1824. #define AT_STRINGS_BEG  (d == (size1 ? string1 : string2)  ||  !size2)
  1825. #define AT_STRINGS_END  (d == end2)    
  1826.  
  1827. #define AT_WORD_BOUNDARY                        \
  1828.   (AT_STRINGS_BEG || AT_STRINGS_END || IS_A_LETTER (d - 1) != IS_A_LETTER (d))
  1829.  
  1830. /* We have two special cases to check for: 
  1831.      1) if we're past the end of string1, we have to look at the first
  1832.         character in string2;
  1833.      2) if we're before the beginning of string2, we have to look at the
  1834.         last character in string1; we assume there is a string1, so use
  1835.         this in conjunction with AT_STRINGS_BEG.  */
  1836. #define IS_A_LETTER(d)                            \
  1837.   (SYNTAX ((d) == end1 ? *string2 : (d) == string2 - 1 ? *(end1 - 1) : *(d))\
  1838.    == Sword)
  1839.  
  1840.  
  1841. /* Match the pattern described by PBUFP against the virtual
  1842.    concatenation of STRING1 and STRING2, which are of SIZE1 and SIZE2,
  1843.    respectively.  Start the match at index POS in the virtual
  1844.    concatenation of STRING1 and STRING2.  In REGS, return the indices of
  1845.    the virtual concatenation of STRING1 and STRING2 that matched the
  1846.    entire PBUFP->buffer and its contained subexpressions.  Do not
  1847.    consider matching one past the index MSTOP in the virtual
  1848.    concatenation of STRING1 and STRING2.
  1849.  
  1850.    If pbufp->fastmap is nonzero, then it had better be up to date.
  1851.  
  1852.    The reason that the data to match are specified as two components
  1853.    which are to be regarded as concatenated is so this function can be
  1854.    used directly on the contents of an Emacs buffer.
  1855.  
  1856.    -1 is returned if there is no match.  -2 is returned if there is an
  1857.    error (such as match stack overflow).  Otherwise the value is the
  1858.    length of the substring which was matched.  */
  1859.  
  1860. int
  1861. re_match_2 (pbufp, string1_arg, size1, string2_arg, size2, pos, regs, mstop)
  1862.      struct re_pattern_buffer *pbufp;
  1863.      char *string1_arg, *string2_arg;
  1864.      int size1, size2;
  1865.      int pos;
  1866.      struct re_registers *regs;
  1867.      int mstop;
  1868. {
  1869.   register unsigned char *p = (unsigned char *) pbufp->buffer;
  1870.  
  1871.   /* Pointer to beyond end of buffer.  */
  1872.   register unsigned char *pend = p + pbufp->used;
  1873.  
  1874.   unsigned char *string1 = (unsigned char *) string1_arg;
  1875.   unsigned char *string2 = (unsigned char *) string2_arg;
  1876.   unsigned char *end1;        /* Just past end of first string.  */
  1877.   unsigned char *end2;        /* Just past end of second string.  */
  1878.  
  1879.   /* Pointers into string1 and string2, just past the last characters in
  1880.      each to consider matching.  */
  1881.   unsigned char *end_match_1, *end_match_2;
  1882.  
  1883.   register unsigned char *d, *dend;
  1884.   register int mcnt;            /* Multipurpose.  */
  1885.   unsigned char *translate = (unsigned char *) pbufp->translate;
  1886.   unsigned is_a_jump_n = 0;
  1887.  
  1888.  /* Failure point stack.  Each place that can handle a failure further
  1889.     down the line pushes a failure point on this stack.  It consists of
  1890.     restart, regend, and reg_info for all registers corresponding to the
  1891.     subexpressions we're currently inside, plus the number of such
  1892.     registers, and, finally, two char *'s.  The first char * is where to
  1893.     resume scanning the pattern; the second one is where to resume
  1894.     scanning the strings.  If the latter is zero, the failure point is a
  1895.     ``dummy''; if a failure happens and the failure point is a dummy, it
  1896.     gets discarded and the next next one is tried.  */
  1897.  
  1898.   unsigned char *initial_stack[MAX_NUM_FAILURE_ITEMS * NFAILURES];
  1899.   unsigned char **stackb = initial_stack;
  1900.   unsigned char **stackp = stackb;
  1901.   unsigned char **stacke = &stackb[MAX_NUM_FAILURE_ITEMS * NFAILURES];
  1902.  
  1903.  
  1904.   /* Information on the contents of registers. These are pointers into
  1905.      the input strings; they record just what was matched (on this
  1906.      attempt) by a subexpression part of the pattern, that is, the
  1907.      regnum-th regstart pointer points to where in the pattern we began
  1908.      matching and the regnum-th regend points to right after where we
  1909.      stopped matching the regnum-th subexpression.  (The zeroth register
  1910.      keeps track of what the whole pattern matches.)  */
  1911.      
  1912.   unsigned char *regstart[RE_NREGS];
  1913.   unsigned char *regend[RE_NREGS];
  1914.  
  1915.   /* The is_active field of reg_info helps us keep track of which (possibly
  1916.      nested) subexpressions we are currently in. The matched_something
  1917.      field of reg_info[reg_num] helps us tell whether or not we have
  1918.      matched any of the pattern so far this time through the reg_num-th
  1919.      subexpression.  These two fields get reset each time through any
  1920.      loop their register is in.  */
  1921.  
  1922.   struct register_info reg_info[RE_NREGS];
  1923.  
  1924.  
  1925.   /* The following record the register info as found in the above
  1926.      variables when we find a match better than any we've seen before. 
  1927.      This happens as we backtrack through the failure points, which in
  1928.      turn happens only if we have not yet matched the entire string.  */
  1929.  
  1930.   unsigned best_regs_set = 0;
  1931.   unsigned char *best_regstart[RE_NREGS];
  1932.   unsigned char *best_regend[RE_NREGS];
  1933.  
  1934.   /* Initialize subexpression text positions to -1 to mark ones that no
  1935.      \( or ( and \) or ) has been seen for. Also set all registers to
  1936.      inactive and mark them as not having matched anything or ever
  1937.      failed.  */
  1938.   for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
  1939.     {
  1940.       regstart[mcnt] = regend[mcnt] = (unsigned char *) -1;
  1941.       IS_ACTIVE (reg_info[mcnt]) = 0;
  1942.       MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  1943.     }
  1944.   
  1945.   if (regs)
  1946.     for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
  1947.       regs->start[mcnt] = regs->end[mcnt] = -1;
  1948.  
  1949.   /* Set up pointers to ends of strings.
  1950.      Don't allow the second string to be empty unless both are empty.  */
  1951.   if (size2 == 0)
  1952.     {
  1953.       string2 = string1;
  1954.       size2 = size1;
  1955.       string1 = 0;
  1956.       size1 = 0;
  1957.     }
  1958.   end1 = string1 + size1;
  1959.   end2 = string2 + size2;
  1960.  
  1961.   /* Compute where to stop matching, within the two strings.  */
  1962.   if (mstop <= size1)
  1963.     {
  1964.       end_match_1 = string1 + mstop;
  1965.       end_match_2 = string2;
  1966.     }
  1967.   else
  1968.     {
  1969.       end_match_1 = end1;
  1970.       end_match_2 = string2 + mstop - size1;
  1971.     }
  1972.  
  1973.   /* `p' scans through the pattern as `d' scans through the data. `dend'
  1974.      is the end of the input string that `d' points within. `d' is
  1975.      advanced into the following input string whenever necessary, but
  1976.      this happens before fetching; therefore, at the beginning of the
  1977.      loop, `d' can be pointing at the end of a string, but it cannot
  1978.      equal string2.  */
  1979.  
  1980.   if (size1 != 0 && pos <= size1)
  1981.     d = string1 + pos, dend = end_match_1;
  1982.   else
  1983.     d = string2 + pos - size1, dend = end_match_2;
  1984.  
  1985.  
  1986.   /* This loops over pattern commands.  It exits by returning from the
  1987.      function if match is complete, or it drops through if match fails
  1988.      at this starting point in the input data.  */
  1989.  
  1990.   while (1)
  1991.     {
  1992.       is_a_jump_n = 0;
  1993.       /* End of pattern means we might have succeeded.  */
  1994.       if (p == pend)
  1995.     {
  1996.       /* If not end of string, try backtracking.  Otherwise done.  */
  1997.           if (d != end_match_2)
  1998.         {
  1999.               if (stackp != stackb)
  2000.                 {
  2001.                   /* More failure points to try.  */
  2002.  
  2003.                   unsigned in_same_string = 
  2004.                           IS_IN_FIRST_STRING (best_regend[0]) 
  2005.                         == MATCHING_IN_FIRST_STRING;
  2006.  
  2007.                   /* If exceeds best match so far, save it.  */
  2008.                   if (! best_regs_set
  2009.                       || (in_same_string && d > best_regend[0])
  2010.                       || (! in_same_string && ! MATCHING_IN_FIRST_STRING))
  2011.                     {
  2012.                       best_regs_set = 1;
  2013.                       best_regend[0] = d;    /* Never use regstart[0].  */
  2014.                       
  2015.                       for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  2016.                         {
  2017.                           best_regstart[mcnt] = regstart[mcnt];
  2018.                           best_regend[mcnt] = regend[mcnt];
  2019.                         }
  2020.                     }
  2021.                   goto fail;           
  2022.                 }
  2023.               /* If no failure points, don't restore garbage.  */
  2024.               else if (best_regs_set)   
  2025.                 {
  2026.           restore_best_regs:
  2027.                   /* Restore best match.  */
  2028.                   d = best_regend[0];
  2029.                   
  2030.           for (mcnt = 0; mcnt < RE_NREGS; mcnt++)
  2031.             {
  2032.               regstart[mcnt] = best_regstart[mcnt];
  2033.               regend[mcnt] = best_regend[mcnt];
  2034.             }
  2035.                 }
  2036.             }
  2037.  
  2038.       /* If caller wants register contents data back, convert it 
  2039.          to indices.  */
  2040.       if (regs)
  2041.         {
  2042.           regs->start[0] = pos;
  2043.           if (MATCHING_IN_FIRST_STRING)
  2044.         regs->end[0] = d - string1;
  2045.           else
  2046.         regs->end[0] = d - string2 + size1;
  2047.           for (mcnt = 1; mcnt < RE_NREGS; mcnt++)
  2048.         {
  2049.           if (regend[mcnt] == (unsigned char *) -1)
  2050.             {
  2051.               regs->start[mcnt] = -1;
  2052.               regs->end[mcnt] = -1;
  2053.               continue;
  2054.             }
  2055.           if (IS_IN_FIRST_STRING (regstart[mcnt]))
  2056.             regs->start[mcnt] = regstart[mcnt] - string1;
  2057.           else
  2058.             regs->start[mcnt] = regstart[mcnt] - string2 + size1;
  2059.                     
  2060.           if (IS_IN_FIRST_STRING (regend[mcnt]))
  2061.             regs->end[mcnt] = regend[mcnt] - string1;
  2062.           else
  2063.             regs->end[mcnt] = regend[mcnt] - string2 + size1;
  2064.         }
  2065.         }
  2066.       return d - pos - (MATCHING_IN_FIRST_STRING 
  2067.                 ? string1 
  2068.                 : string2 - size1);
  2069.         }
  2070.  
  2071.       /* Otherwise match next pattern command.  */
  2072. #ifdef SWITCH_ENUM_BUG
  2073.       switch ((int) ((enum regexpcode) *p++))
  2074. #else
  2075.       switch ((enum regexpcode) *p++)
  2076. #endif
  2077.     {
  2078.  
  2079.     /* \( [or `(', as appropriate] is represented by start_memory,
  2080.            \) by stop_memory.  Both of those commands are followed by
  2081.            a register number in the next byte.  The text matched
  2082.            within the \( and \) is recorded under that number.  */
  2083.     case start_memory:
  2084.           regstart[*p] = d;
  2085.           IS_ACTIVE (reg_info[*p]) = 1;
  2086.           MATCHED_SOMETHING (reg_info[*p]) = 0;
  2087.           p++;
  2088.           break;
  2089.  
  2090.     case stop_memory:
  2091.           regend[*p] = d;
  2092.           IS_ACTIVE (reg_info[*p]) = 0;
  2093.  
  2094.           /* If just failed to match something this time around with a sub-
  2095.          expression that's in a loop, try to force exit from the loop.  */
  2096.           if ((! MATCHED_SOMETHING (reg_info[*p])
  2097.            || (enum regexpcode) p[-3] == start_memory)
  2098.           && (p + 1) != pend)              
  2099.             {
  2100.           register unsigned char *p2 = p + 1;
  2101.               mcnt = 0;
  2102.               switch (*p2++)
  2103.                 {
  2104.                   case jump_n:
  2105.             is_a_jump_n = 1;
  2106.                   case finalize_jump:
  2107.           case maybe_finalize_jump:
  2108.           case jump:
  2109.           case dummy_failure_jump:
  2110.                     EXTRACT_NUMBER_AND_INCR (mcnt, p2);
  2111.             if (is_a_jump_n)
  2112.               p2 += 2;
  2113.                     break;
  2114.                 }
  2115.           p2 += mcnt;
  2116.         
  2117.               /* If the next operation is a jump backwards in the pattern
  2118.              to an on_failure_jump, exit from the loop by forcing a
  2119.                  failure after pushing on the stack the on_failure_jump's 
  2120.                  jump in the pattern, and d.  */
  2121.           if (mcnt < 0 && (enum regexpcode) *p2++ == on_failure_jump)
  2122.         {
  2123.                   EXTRACT_NUMBER_AND_INCR (mcnt, p2);
  2124.                   PUSH_FAILURE_POINT (p2 + mcnt, d);
  2125.                   goto fail;
  2126.                 }
  2127.             }
  2128.           p++;
  2129.           break;
  2130.  
  2131.     /* \<digit> has been turned into a `duplicate' command which is
  2132.            followed by the numeric value of <digit> as the register number.  */
  2133.         case duplicate:
  2134.       {
  2135.         int regno = *p++;   /* Get which register to match against */
  2136.         register unsigned char *d2, *dend2;
  2137.  
  2138.         /* Where in input to try to start matching.  */
  2139.             d2 = regstart[regno];
  2140.             
  2141.             /* Where to stop matching; if both the place to start and
  2142.                the place to stop matching are in the same string, then
  2143.                set to the place to stop, otherwise, for now have to use
  2144.                the end of the first string.  */
  2145.  
  2146.             dend2 = ((IS_IN_FIRST_STRING (regstart[regno]) 
  2147.               == IS_IN_FIRST_STRING (regend[regno]))
  2148.              ? regend[regno] : end_match_1);
  2149.         while (1)
  2150.           {
  2151.         /* If necessary, advance to next segment in register
  2152.                    contents.  */
  2153.         while (d2 == dend2)
  2154.           {
  2155.             if (dend2 == end_match_2) break;
  2156.             if (dend2 == regend[regno]) break;
  2157.             d2 = string2, dend2 = regend[regno];  /* end of string1 => advance to string2. */
  2158.           }
  2159.         /* At end of register contents => success */
  2160.         if (d2 == dend2) break;
  2161.  
  2162.         /* If necessary, advance to next segment in data.  */
  2163.         PREFETCH;
  2164.  
  2165.         /* How many characters left in this segment to match.  */
  2166.         mcnt = dend - d;
  2167.                 
  2168.         /* Want how many consecutive characters we can match in
  2169.                    one shot, so, if necessary, adjust the count.  */
  2170.                 if (mcnt > dend2 - d2)
  2171.           mcnt = dend2 - d2;
  2172.                   
  2173.         /* Compare that many; failure if mismatch, else move
  2174.                    past them.  */
  2175.         if (translate 
  2176.                     ? bcmp_translate (d, d2, mcnt, translate) 
  2177.                     : bcmp (d, d2, mcnt))
  2178.           goto fail;
  2179.         d += mcnt, d2 += mcnt;
  2180.           }
  2181.       }
  2182.       break;
  2183.  
  2184.     case anychar:
  2185.       PREFETCH;      /* Fetch a data character. */
  2186.       /* Match anything but a newline, maybe even a null.  */
  2187.       if ((translate ? translate[*d] : *d) == '\n'
  2188.               || ((obscure_syntax & RE_DOT_NOT_NULL) 
  2189.                   && (translate ? translate[*d] : *d) == '\000'))
  2190.         goto fail;
  2191.       SET_REGS_MATCHED;
  2192.           d++;
  2193.       break;
  2194.  
  2195.     case charset:
  2196.     case charset_not:
  2197.       {
  2198.         int not = 0;        /* Nonzero for charset_not.  */
  2199.         register int c;
  2200.         if (*(p - 1) == (unsigned char) charset_not)
  2201.           not = 1;
  2202.  
  2203.         PREFETCH;        /* Fetch a data character. */
  2204.  
  2205.         if (translate)
  2206.           c = translate[*d];
  2207.         else
  2208.           c = *d;
  2209.  
  2210.         if (c < *p * BYTEWIDTH
  2211.         && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  2212.           not = !not;
  2213.  
  2214.         p += 1 + *p;
  2215.  
  2216.         if (!not) goto fail;
  2217.         SET_REGS_MATCHED;
  2218.             d++;
  2219.         break;
  2220.       }
  2221.  
  2222.     case begline:
  2223.           if ((size1 != 0 && d == string1)
  2224.               || (size1 == 0 && size2 != 0 && d == string2)
  2225.               || (d && d[-1] == '\n')
  2226.               || (size1 == 0 && size2 == 0))
  2227.             break;
  2228.           else
  2229.             goto fail;
  2230.             
  2231.     case endline:
  2232.       if (d == end2
  2233.           || (d == end1 ? (size2 == 0 || *string2 == '\n') : *d == '\n'))
  2234.         break;
  2235.       goto fail;
  2236.  
  2237.     /* `or' constructs are handled by starting each alternative with
  2238.            an on_failure_jump that points to the start of the next
  2239.            alternative.  Each alternative except the last ends with a
  2240.            jump to the joining point.  (Actually, each jump except for
  2241.            the last one really jumps to the following jump, because
  2242.            tensioning the jumps is a hassle.)  */
  2243.  
  2244.     /* The start of a stupid repeat has an on_failure_jump that points
  2245.        past the end of the repeat text. This makes a failure point so 
  2246.            that on failure to match a repetition, matching restarts past
  2247.            as many repetitions have been found with no way to fail and
  2248.            look for another one.  */
  2249.  
  2250.     /* A smart repeat is similar but loops back to the on_failure_jump
  2251.        so that each repetition makes another failure point.  */
  2252.  
  2253.     case on_failure_jump:
  2254.         on_failure:
  2255.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2256.           PUSH_FAILURE_POINT (p + mcnt, d);
  2257.           break;
  2258.  
  2259.     /* The end of a smart repeat has a maybe_finalize_jump back.
  2260.        Change it either to a finalize_jump or an ordinary jump.  */
  2261.     case maybe_finalize_jump:
  2262.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2263.       {
  2264.         register unsigned char *p2 = p;
  2265.         /* Compare what follows with the beginning of the repeat.
  2266.            If we can establish that there is nothing that they would
  2267.            both match, we can change to finalize_jump.  */
  2268.         while (p2 + 1 != pend
  2269.            && (*p2 == (unsigned char) stop_memory
  2270.                || *p2 == (unsigned char) start_memory))
  2271.           p2 += 2;                /* Skip over reg number.  */
  2272.         if (p2 == pend)
  2273.           p[-3] = (unsigned char) finalize_jump;
  2274.         else if (*p2 == (unsigned char) exactn
  2275.              || *p2 == (unsigned char) endline)
  2276.           {
  2277.         register int c = *p2 == (unsigned char) endline ? '\n' : p2[2];
  2278.         register unsigned char *p1 = p + mcnt;
  2279.         /* p1[0] ... p1[2] are an on_failure_jump.
  2280.            Examine what follows that.  */
  2281.         if (p1[3] == (unsigned char) exactn && p1[5] != c)
  2282.           p[-3] = (unsigned char) finalize_jump;
  2283.         else if (p1[3] == (unsigned char) charset
  2284.              || p1[3] == (unsigned char) charset_not)
  2285.           {
  2286.             int not = p1[3] == (unsigned char) charset_not;
  2287.             if (c < p1[4] * BYTEWIDTH
  2288.             && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  2289.               not = !not;
  2290.             /* `not' is 1 if c would match.  */
  2291.             /* That means it is not safe to finalize.  */
  2292.             if (!not)
  2293.               p[-3] = (unsigned char) finalize_jump;
  2294.           }
  2295.           }
  2296.       }
  2297.       p -= 2;        /* Point at relative address again.  */
  2298.       if (p[-1] != (unsigned char) finalize_jump)
  2299.         {
  2300.           p[-1] = (unsigned char) jump;    
  2301.           goto nofinalize;
  2302.         }
  2303.         /* Note fall through.  */
  2304.  
  2305.     /* The end of a stupid repeat has a finalize_jump back to the
  2306.            start, where another failure point will be made which will
  2307.            point to after all the repetitions found so far.  */
  2308.  
  2309.         /* Take off failure points put on by matching on_failure_jump 
  2310.            because didn't fail.  Also remove the register information
  2311.            put on by the on_failure_jump.  */
  2312.         case finalize_jump:
  2313.           POP_FAILURE_POINT ();
  2314.         /* Note fall through.  */
  2315.         
  2316.     /* Jump without taking off any failure points.  */
  2317.         case jump:
  2318.     nofinalize:
  2319.       EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2320.       p += mcnt;
  2321.       break;
  2322.  
  2323.         case dummy_failure_jump:
  2324.           /* Normally, the on_failure_jump pushes a failure point, which
  2325.              then gets popped at finalize_jump.  We will end up at
  2326.              finalize_jump, also, and with a pattern of, say, `a+', we
  2327.              are skipping over the on_failure_jump, so we have to push
  2328.              something meaningless for finalize_jump to pop.  */
  2329.           PUSH_FAILURE_POINT (0, 0);
  2330.           goto nofinalize;
  2331.  
  2332.  
  2333.         /* Have to succeed matching what follows at least n times.  Then
  2334.           just handle like an on_failure_jump.  */
  2335.         case succeed_n: 
  2336.           EXTRACT_NUMBER (mcnt, p + 2);
  2337.           /* Originally, this is how many times we HAVE to succeed.  */
  2338.           if (mcnt)
  2339.             {
  2340.                mcnt--;
  2341.            p += 2;
  2342.                STORE_NUMBER_AND_INCR (p, mcnt);
  2343.             }
  2344.       else if (mcnt == 0)
  2345.             {
  2346.           p[2] = unused;
  2347.               p[3] = unused;
  2348.               goto on_failure;
  2349.             }
  2350.           else
  2351.         { 
  2352.               fprintf (stderr, "regex: the succeed_n's n is not set.\n");
  2353.               exit (1);
  2354.         }
  2355.           break;
  2356.         
  2357.         case jump_n: 
  2358.           EXTRACT_NUMBER (mcnt, p + 2);
  2359.           /* Originally, this is how many times we CAN jump.  */
  2360.           if (mcnt)
  2361.             {
  2362.                mcnt--;
  2363.                STORE_NUMBER(p + 2, mcnt);
  2364.            goto nofinalize;         /* Do the jump without taking off
  2365.                             any failure points.  */
  2366.             }
  2367.           /* If don't have to jump any more, skip over the rest of command.  */
  2368.       else      
  2369.         p += 4;             
  2370.           break;
  2371.         
  2372.     case set_number_at:
  2373.       {
  2374.           register unsigned char *p1;
  2375.  
  2376.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2377.             p1 = p + mcnt;
  2378.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  2379.         STORE_NUMBER (p1, mcnt);
  2380.             break;
  2381.           }
  2382.  
  2383.         /* Ignore these.  Used to ignore the n of succeed_n's which
  2384.            currently have n == 0.  */
  2385.         case unused:
  2386.           break;
  2387.  
  2388.         case wordbound:
  2389.       if (AT_WORD_BOUNDARY)
  2390.         break;
  2391.       goto fail;
  2392.  
  2393.     case notwordbound:
  2394.       if (AT_WORD_BOUNDARY)
  2395.         goto fail;
  2396.       break;
  2397.  
  2398.     case wordbeg:
  2399.           /* Have to check if AT_STRINGS_BEG before looking at d - 1.  */
  2400.       if (IS_A_LETTER (d) && (AT_STRINGS_BEG || !IS_A_LETTER (d - 1)))
  2401.         break;
  2402.       goto fail;
  2403.  
  2404.     case wordend:
  2405.           /* Have to check if AT_STRINGS_BEG before looking at d - 1.  */
  2406.       if (!AT_STRINGS_BEG && IS_A_LETTER (d - 1) 
  2407.               && (!IS_A_LETTER (d) || AT_STRINGS_END))
  2408.         break;
  2409.       goto fail;
  2410.  
  2411. #ifdef emacs
  2412.     case before_dot:
  2413.       if (PTR_CHAR_POS (d) >= point)
  2414.         goto fail;
  2415.       break;
  2416.  
  2417.     case at_dot:
  2418.       if (PTR_CHAR_POS (d) != point)
  2419.         goto fail;
  2420.       break;
  2421.  
  2422.     case after_dot:
  2423.       if (PTR_CHAR_POS (d) <= point)
  2424.         goto fail;
  2425.       break;
  2426.  
  2427.     case wordchar:
  2428.       mcnt = (int) Sword;
  2429.       goto matchsyntax;
  2430.  
  2431.     case syntaxspec:
  2432.       mcnt = *p++;
  2433.     matchsyntax:
  2434.       PREFETCH;
  2435.       if (SYNTAX (*d++) != (enum syntaxcode) mcnt) goto fail;
  2436.           SET_REGS_MATCHED;
  2437.       break;
  2438.       
  2439.     case notwordchar:
  2440.       mcnt = (int) Sword;
  2441.       goto matchnotsyntax;
  2442.  
  2443.     case notsyntaxspec:
  2444.       mcnt = *p++;
  2445.     matchnotsyntax:
  2446.       PREFETCH;
  2447.       if (SYNTAX (*d++) == (enum syntaxcode) mcnt) goto fail;
  2448.       SET_REGS_MATCHED;
  2449.           break;
  2450.  
  2451. #else /* not emacs */
  2452.  
  2453.     case wordchar:
  2454.       PREFETCH;
  2455.           if (!IS_A_LETTER (d))
  2456.             goto fail;
  2457.       SET_REGS_MATCHED;
  2458.       break;
  2459.       
  2460.     case notwordchar:
  2461.       PREFETCH;
  2462.       if (IS_A_LETTER (d))
  2463.             goto fail;
  2464.           SET_REGS_MATCHED;
  2465.       break;
  2466.  
  2467. #endif /* not emacs */
  2468.  
  2469.     case begbuf:
  2470.           if (AT_STRINGS_BEG)
  2471.             break;
  2472.           goto fail;
  2473.  
  2474.         case endbuf:
  2475.       if (AT_STRINGS_END)
  2476.         break;
  2477.       goto fail;
  2478.  
  2479.     case exactn:
  2480.       /* Match the next few pattern characters exactly.
  2481.          mcnt is how many characters to match.  */
  2482.       mcnt = *p++;
  2483.       /* This is written out as an if-else so we don't waste time
  2484.              testing `translate' inside the loop.  */
  2485.           if (translate)
  2486.         {
  2487.           do
  2488.         {
  2489.           PREFETCH;
  2490.           if (translate[*d++] != *p++) goto fail;
  2491.         }
  2492.           while (--mcnt);
  2493.         }
  2494.       else
  2495.         {
  2496.           do
  2497.         {
  2498.           PREFETCH;
  2499.           if (*d++ != *p++) goto fail;
  2500.         }
  2501.           while (--mcnt);
  2502.         }
  2503.       SET_REGS_MATCHED;
  2504.           break;
  2505.     }
  2506.       continue;  /* Successfully executed one pattern command; keep going.  */
  2507.  
  2508.     /* Jump here if any matching operation fails. */
  2509.     fail:
  2510.       if (stackp != stackb)
  2511.     /* A restart point is known.  Restart there and pop it. */
  2512.     {
  2513.           short last_used_reg, this_reg;
  2514.           
  2515.           /* If this failure point is from a dummy_failure_point, just
  2516.              skip it.  */
  2517.       if (!stackp[-2])
  2518.             {
  2519.               POP_FAILURE_POINT ();
  2520.               goto fail;
  2521.             }
  2522.  
  2523.           d = *--stackp;
  2524.       p = *--stackp;
  2525.           if (d >= string1 && d <= end1)
  2526.         dend = end_match_1;
  2527.           /* Restore register info.  */
  2528.           last_used_reg = (short) *--stackp;
  2529.           
  2530.           /* Make the ones that weren't saved -1 or 0 again.  */
  2531.           for (this_reg = RE_NREGS - 1; this_reg > last_used_reg; this_reg--)
  2532.             {
  2533.               regend[this_reg] = (unsigned char *) -1;
  2534.               regstart[this_reg] = (unsigned char *) -1;
  2535.               IS_ACTIVE (reg_info[this_reg]) = 0;
  2536.               MATCHED_SOMETHING (reg_info[this_reg]) = 0;
  2537.             }
  2538.           
  2539.           /* And restore the rest from the stack.  */
  2540.           for ( ; this_reg > 0; this_reg--)
  2541.             {
  2542.               reg_info[this_reg] = *(struct register_info *) *--stackp;
  2543.               regend[this_reg] = *--stackp;
  2544.               regstart[this_reg] = *--stackp;
  2545.             }
  2546.     }
  2547.       else
  2548.         break;   /* Matching at this starting point really fails.  */
  2549.     }
  2550.  
  2551.   if (best_regs_set)
  2552.     goto restore_best_regs;
  2553.   return -1;                     /* Failure to match.  */
  2554. }
  2555.  
  2556.  
  2557. static int
  2558. bcmp_translate (s1, s2, len, translate)
  2559.      unsigned char *s1, *s2;
  2560.      register int len;
  2561.      unsigned char *translate;
  2562. {
  2563.   register unsigned char *p1 = s1, *p2 = s2;
  2564.   while (len)
  2565.     {
  2566.       if (translate [*p1++] != translate [*p2++]) return 1;
  2567.       len--;
  2568.     }
  2569.   return 0;
  2570. }
  2571.  
  2572.  
  2573.  
  2574. /* Entry points compatible with 4.2 BSD regex library.  */
  2575.  
  2576. #ifndef emacs
  2577.  
  2578. static struct re_pattern_buffer re_comp_buf;
  2579.  
  2580. char *
  2581. re_comp (s)
  2582.      char *s;
  2583. {
  2584.   if (!s)
  2585.     {
  2586.       if (!re_comp_buf.buffer)
  2587.     return "No previous regular expression";
  2588.       return 0;
  2589.     }
  2590.  
  2591.   if (!re_comp_buf.buffer)
  2592.     {
  2593.       if (!(re_comp_buf.buffer = (char *) malloc (200)))
  2594.     return "Memory exhausted";
  2595.       re_comp_buf.allocated = 200;
  2596.       if (!(re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH)))
  2597.     return "Memory exhausted";
  2598.     }
  2599.   return re_compile_pattern (s, strlen (s), &re_comp_buf);
  2600. }
  2601.  
  2602. int
  2603. re_exec (s)
  2604.      char *s;
  2605. {
  2606.   int len = strlen (s);
  2607.   return 0 <= re_search (&re_comp_buf, s, len, 0, len,
  2608.              (struct re_registers *) 0);
  2609. }
  2610. #endif /* not emacs */
  2611.  
  2612.  
  2613.  
  2614. #ifdef test
  2615.  
  2616. #include <stdio.h>
  2617.  
  2618. /* Indexed by a character, gives the upper case equivalent of the
  2619.    character.  */
  2620.  
  2621. char upcase[0400] = 
  2622.   { 000, 001, 002, 003, 004, 005, 006, 007,
  2623.     010, 011, 012, 013, 014, 015, 016, 017,
  2624.     020, 021, 022, 023, 024, 025, 026, 027,
  2625.     030, 031, 032, 033, 034, 035, 036, 037,
  2626.     040, 041, 042, 043, 044, 045, 046, 047,
  2627.     050, 051, 052, 053, 054, 055, 056, 057,
  2628.     060, 061, 062, 063, 064, 065, 066, 067,
  2629.     070, 071, 072, 073, 074, 075, 076, 077,
  2630.     0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  2631.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  2632.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  2633.     0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137,
  2634.     0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  2635.     0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117,
  2636.     0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  2637.     0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177,
  2638.     0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  2639.     0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217,
  2640.     0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227,
  2641.     0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237,
  2642.     0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247,
  2643.     0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  2644.     0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  2645.     0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  2646.     0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  2647.     0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317,
  2648.     0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327,
  2649.     0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  2650.     0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  2651.     0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357,
  2652.     0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  2653.     0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377
  2654.   };
  2655.  
  2656. #ifdef canned
  2657.  
  2658. #include "tests.h"
  2659.  
  2660. typedef enum { extended_test, basic_test } test_type;
  2661.  
  2662. /* Use this to run the tests we've thought of.  */
  2663.  
  2664. void
  2665. main ()
  2666. {
  2667.   test_type t = extended_test;
  2668.  
  2669.   if (t == basic_test)
  2670.     {
  2671.       printf ("Running basic tests:\n\n");
  2672.       test_posix_basic ();
  2673.     }
  2674.   else if (t == extended_test)
  2675.     {
  2676.       printf ("Running extended tests:\n\n");
  2677.       test_posix_extended (); 
  2678.     }
  2679. }
  2680.  
  2681. #else /* not canned */
  2682.  
  2683. /* Use this to run interactive tests.  */
  2684.  
  2685. void
  2686. main (argc, argv)
  2687.      int argc;
  2688.      char **argv;
  2689. {
  2690.   char pat[80];
  2691.   struct re_pattern_buffer buf;
  2692.   int i;
  2693.   char c;
  2694.   char fastmap[(1 << BYTEWIDTH)];
  2695.  
  2696.   /* Allow a command argument to specify the style of syntax.  */
  2697.   if (argc > 1)
  2698.     obscure_syntax = atoi (argv[1]);
  2699.  
  2700.   buf.allocated = 40;
  2701.   buf.buffer = (char *) malloc (buf.allocated);
  2702.   buf.fastmap = fastmap;
  2703.   buf.translate = upcase;
  2704.  
  2705.   while (1)
  2706.     {
  2707.       gets (pat);
  2708.  
  2709.       if (*pat)
  2710.     {
  2711.           re_compile_pattern (pat, strlen(pat), &buf);
  2712.  
  2713.       for (i = 0; i < buf.used; i++)
  2714.         printchar (buf.buffer[i]);
  2715.  
  2716.       putchar ('\n');
  2717.  
  2718.       printf ("%d allocated, %d used.\n", buf.allocated, buf.used);
  2719.  
  2720.       re_compile_fastmap (&buf);
  2721.       printf ("Allowed by fastmap: ");
  2722.       for (i = 0; i < (1 << BYTEWIDTH); i++)
  2723.         if (fastmap[i]) printchar (i);
  2724.       putchar ('\n');
  2725.     }
  2726.  
  2727.       gets (pat);    /* Now read the string to match against */
  2728.  
  2729.       i = re_match (&buf, pat, strlen (pat), 0, 0);
  2730.       printf ("Match value %d.\n", i);
  2731.     }
  2732. }
  2733.  
  2734. #endif
  2735.  
  2736.  
  2737. #ifdef NOTDEF
  2738. print_buf (bufp)
  2739.      struct re_pattern_buffer *bufp;
  2740. {
  2741.   int i;
  2742.  
  2743.   printf ("buf is :\n----------------\n");
  2744.   for (i = 0; i < bufp->used; i++)
  2745.     printchar (bufp->buffer[i]);
  2746.   
  2747.   printf ("\n%d allocated, %d used.\n", bufp->allocated, bufp->used);
  2748.   
  2749.   printf ("Allowed by fastmap: ");
  2750.   for (i = 0; i < (1 << BYTEWIDTH); i++)
  2751.     if (bufp->fastmap[i])
  2752.       printchar (i);
  2753.   printf ("\nAllowed by translate: ");
  2754.   if (bufp->translate)
  2755.     for (i = 0; i < (1 << BYTEWIDTH); i++)
  2756.       if (bufp->translate[i])
  2757.     printchar (i);
  2758.   printf ("\nfastmap is%s accurate\n", bufp->fastmap_accurate ? "" : "n't");
  2759.   printf ("can %s be null\n----------", bufp->can_be_null ? "" : "not");
  2760. }
  2761. #endif /* NOTDEF */
  2762.  
  2763. printchar (c)
  2764.      char c;
  2765. {
  2766.   if (c < 040 || c >= 0177)
  2767.     {
  2768.       putchar ('\\');
  2769.       putchar (((c >> 6) & 3) + '0');
  2770.       putchar (((c >> 3) & 7) + '0');
  2771.       putchar ((c & 7) + '0');
  2772.     }
  2773.   else
  2774.     putchar (c);
  2775. }
  2776.  
  2777. error (string)
  2778.      char *string;
  2779. {
  2780.   puts (string);
  2781.   exit (1);
  2782. }
  2783. #endif /* test */
  2784.